登录
首页 >  Golang >  Go问答

更新 1.1 在文件夹内运行 Go 文件和 JSON 文件时出现问题

来源:stackoverflow

时间:2024-04-27 13:27:39 322浏览 收藏

在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是Golang学习者,那么本文《更新 1.1 在文件夹内运行 Go 文件和 JSON 文件时出现问题》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

更新1.1

收到有关更改处理程序路径的有用反馈后,我在应用程序中使用了该反馈,但奇怪的是我仍然收到一些错误提要。我正在尝试自己解决这个问题,但我想我可以与大家分享。这也是我取消现在给出的答案的原因。另外,有些答案现在让我感到困惑,无法理解这对我来说是如何工作的。

package main

import (

    "net/http" // package http provides http client and server implementations.



)

func main(){
    webhost()

}


func webhost(){
    // ik heb hier later nog een mapje voor de html pagina's toegevoegd. 
    http.handle("/go", http.fileserver(http.dir("./1_go_files"))) 
    http.handle("/html", http.fileserver(http.dir("./2_html_pages"))) 
    http.handle("/storage", http.fileserver(http.dir("./3_json_storage"))) 
    // -----------------------------------------------------------------

    http.handlefunc("/homepage", homehandler) 
    http.handlefunc("/selectiepagina", selectiehandler) 
    http.handlefunc("/userinputpagina", userinputhandler) 
    http.handlefunc("/bestelnrpagina", bestelnrhandler)
    http.handlefunc("/statuspagina", statushandler)  




    //fileserver returns a handler that serves http requests with the contents of the file system rooted at root..

    http.handle("/4_css_styling/", http.stripprefix("/4_css_styling/", http.fileserver(http.dir("css")))) // css styling map.
    http.handle("/5_pizza_img/", http.stripprefix("/5_pizza_img/", http.fileserver(http.dir("img")))) // image mapje.


}

所以这里出现了新的恐慌(对不起,文字墙,我不知道要遗漏什么,所以我只是复制粘贴这里的所有内容:

2018/11/12 10:07:11 http: panic serving [::1]:58084: runtime error: invalid memory address or nil pointer dereference
    goroutine 4 [running]:
    net/http.(*conn).serve.func1(0xc0000de000)
            C:/tools/go/src/net/http/server.go:1746 +0xd7
    panic(0x73a9c0, 0xa34b30)
            C:/tools/go/src/runtime/panic.go:513 +0x1c7
    html/template.(*Template).escape(0x0, 0x0, 0x0)
            C:/tools/go/src/html/template/template.go:95 +0x35
    html/template.(*Template).Execute(0x0, 0x7f7580, 0xc0001160e0, 0x71ac40, 0x7f5300, 0xc000106120, 0x3)
            C:/tools/go/src/html/template/template.go:119 +0x36
    main.Homehandler(0x7f9f60, 0xc0001160e0, 0xc000124000)
            C:/Users/Taavetti/Documents/Go/Sources/src/Projects/VillaBrutus_Main/1_Go_Files/0_Homepagina.go:14 +0xc8
    net/http.HandlerFunc.ServeHTTP(0x7b3ca8, 0x7f9f60, 0xc0001160e0, 0xc000124000)
            C:/tools/go/src/net/http/server.go:1964 +0x4b
    net/http.(*ServeMux).ServeHTTP(0xa438e0, 0x7f9f60, 0xc0001160e0, 0xc000124000)
            C:/tools/go/src/net/http/server.go:2361 +0x12e
    net/http.serverHandler.ServeHTTP(0xc00004b040, 0x7f9f60, 0xc0001160e0, 0xc000124000)
            C:/tools/go/src/net/http/server.go:2741 +0xb2
    net/http.(*conn).serve(0xc0000de000, 0x7fa120, 0xc0000320c0)
            C:/tools/go/src/net/http/server.go:1847 +0x64d
    created by net/http.(*Server).Serve
            C:/tools/go/src/net/http/server.go:2851 +0x2fc
    2018/11/12 10:07:11 http: panic serving [::1]:58087: runtime error: invalid memory address or nil pointer dereference
    goroutine 19 [running]:
    net/http.(*conn).serve.func1(0xc00004cf00)
            C:/tools/go/src/net/http/server.go:1746 +0xd7
    panic(0x73a9c0, 0xa34b30)
            C:/tools/go/src/runtime/panic.go:513 +0x1c7
    html/template.(*Template).escape(0x0, 0x0, 0x0)
            C:/tools/go/src/html/template/template.go:95 +0x35
    html/template.(*Template).Execute(0x0, 0x7f7580, 0xc0001161c0, 0x71ac40, 0x7f5300, 0xc0001061e0, 0x3)
            C:/tools/go/src/html/template/template.go:119 +0x36
    main.Homehandler(0x7f9f60, 0xc0001161c0, 0xc000126000)
            C:/Users/Taavetti/Documents/Go/Sources/src/Projects/VillaBrutus_Main/1_Go_Files/0_Homepagina.go:14 +0xc8
    net/http.HandlerFunc.ServeHTTP(0x7b3ca8, 0x7f9f60, 0xc0001161c0, 0xc000126000)
            C:/tools/go/src/net/http/server.go:1964 +0x4b
    net/http.(*ServeMux).ServeHTTP(0xa438e0, 0x7f9f60, 0xc0001161c0, 0xc000126000)
            C:/tools/go/src/net/http/server.go:2361 +0x12e
    net/http.serverHandler.ServeHTTP(0xc00004b040, 0x7f9f60, 0xc0001161c0, 0xc000126000)
            C:/tools/go/src/net/http/server.go:2741 +0xb2
    net/http.(*conn).serve(0xc00004cf00, 0x7fa120, 0xc00004e240)
            C:/tools/go/src/net/http/server.go:1847 +0x64d
    created by net/http.(*Server).Serve
            C:/tools/go/src/net/http/server.go:2851 +0x2fc
    2018/11/12 10:07:11 http: panic serving [::1]:58088: runtime error: invalid memory address or nil pointer dereference
    goroutine 37 [running]:
    net/http.(*conn).serve.func1(0xc000172000)
            C:/tools/go/src/net/http/server.go:1746 +0xd7
    panic(0x73a9c0, 0xa34b30)
            C:/tools/go/src/runtime/panic.go:513 +0x1c7
    html/template.(*Template).escape(0x0, 0x0, 0x0)
            C:/tools/go/src/html/template/template.go:95 +0x35
    html/template.(*Template).Execute(0x0, 0x7f7580, 0xc0001162a0, 0x71ac40, 0x7f5300, 0xc000106360, 0x3)
            C:/tools/go/src/html/template/template.go:119 +0x36
    main.Homehandler(0x7f9f60, 0xc0001162a0, 0xc000124100)
            C:/Users/Taavetti/Documents/Go/Sources/src/Projects/VillaBrutus_Main/1_Go_Files/0_Homepagina.go:14 +0xc8
    net/http.HandlerFunc.ServeHTTP(0x7b3ca8, 0x7f9f60, 0xc0001162a0, 0xc000124100)
            C:/tools/go/src/net/http/server.go:1964 +0x4b
    net/http.(*ServeMux).ServeHTTP(0xa438e0, 0x7f9f60, 0xc0001162a0, 0xc000124100)
            C:/tools/go/src/net/http/server.go:2361 +0x12e
    net/http.serverHandler.ServeHTTP(0xc00004b040, 0x7f9f60, 0xc0001162a0, 0xc000124100)
            C:/tools/go/src/net/http/server.go:2741 +0xb2
    net/http.(*conn).serve(0xc000172000, 0x7fa120, 0xc000110240)
            C:/tools/go/src/net/http/server.go:1847 +0x64d
    created by net/http.(*Server).Serve
            C:/tools/go/src/net/http/server.go:2851 +0x2fc
    2018/11/12 10:07:11 http: panic serving [::1]:58089: runtime error: invalid memory address or nil pointer dereference
    goroutine 39 [running]:
    net/http.(*conn).serve.func1(0xc0001720a0)
            C:/tools/go/src/net/http/server.go:1746 +0xd7
    panic(0x73a9c0, 0xa34b30)
            C:/tools/go/src/runtime/panic.go:513 +0x1c7
    html/template.(*Template).escape(0x0, 0x0, 0x0)
            C:/tools/go/src/html/template/template.go:95 +0x35
    html/template.(*Template).Execute(0x0, 0x7f7580, 0xc000116380, 0x71ac40, 0x7f5300, 0xc0001064e0, 0x3)
            C:/tools/go/src/html/template/template.go:119 +0x36
    main.Homehandler(0x7f9f60, 0xc000116380, 0xc000124200)
            C:/Users/Taavetti/Documents/Go/Sources/src/Projects/VillaBrutus_Main/1_Go_Files/0_Homepagina.go:14 +0xc8
    net/http.HandlerFunc.ServeHTTP(0x7b3ca8, 0x7f9f60, 0xc000116380, 0xc000124200)
            C:/tools/go/src/net/http/server.go:1964 +0x4b
    net/http.(*ServeMux).ServeHTTP(0xa438e0, 0x7f9f60, 0xc000116380, 0xc000124200)
            C:/tools/go/src/net/http/server.go:2361 +0x12e
    net/http.serverHandler.ServeHTTP(0xc00004b040, 0x7f9f60, 0xc000116380, 0xc000124200)
            C:/tools/go/src/net/http/server.go:2741 +0xb2
    net/http.(*conn).serve(0xc0001720a0, 0x7fa120, 0xc000110340)
            C:/tools/go/src/net/http/server.go:1847 +0x64d
    created by net/http.(*Server).Serve
            C:/tools/go/src/net/http/server.go:2851 +0x2fc
    2018/11/12 10:07:12 http: panic serving [::1]:58090: runtime error: invalid memory address or nil pointer dereference
    goroutine 20 [running]:
    net/http.(*conn).serve.func1(0xc00004cfa0)
            C:/tools/go/src/net/http/server.go:1746 +0xd7
    panic(0x73a9c0, 0xa34b30)
            C:/tools/go/src/runtime/panic.go:513 +0x1c7
    html/template.(*Template).escape(0x0, 0x0, 0x0)
            C:/tools/go/src/html/template/template.go:95 +0x35
    html/template.(*Template).Execute(0x0, 0x7f7580, 0xc000116460, 0x71ac40, 0x7f5300, 0xc000106660, 0x3)
            C:/tools/go/src/html/template/template.go:119 +0x36
    main.Homehandler(0x7f9f60, 0xc000116460, 0xc000126100)
            C:/Users/Taavetti/Documents/Go/Sources/src/Projects/VillaBrutus_Main/1_Go_Files/0_Homepagina.go:14 +0xc8
    net/http.HandlerFunc.ServeHTTP(0x7b3ca8, 0x7f9f60, 0xc000116460, 0xc000126100)
            C:/tools/go/src/net/http/server.go:1964 +0x4b
    net/http.(*ServeMux).ServeHTTP(0xa438e0, 0x7f9f60, 0xc000116460, 0xc000126100)
            C:/tools/go/src/net/http/server.go:2361 +0x12e
    net/http.serverHandler.ServeHTTP(0xc00004b040, 0x7f9f60, 0xc000116460, 0xc000126100)
            C:/tools/go/src/net/http/server.go:2741 +0xb2
    net/http.(*conn).serve(0xc00004cfa0, 0x7fa120, 0xc00004e2c0)
            C:/tools/go/src/net/http/server.go:1847 +0x64d
    created by net/http.(*Server).Serve
            C:/tools/go/src/net/http/server.go:2851 +0x2fc
    2018/11/12 10:07:12 http: panic serving [::1]:58091: runtime error: invalid memory address or nil pointer dereference
    goroutine 41 [running]:
    net/http.(*conn).serve.func1(0xc000172140)
            C:/tools/go/src/net/http/server.go:1746 +0xd7
    panic(0x73a9c0, 0xa34b30)
            C:/tools/go/src/runtime/panic.go:513 +0x1c7
    html/template.(*Template).escape(0x0, 0x0, 0x0)
            C:/tools/go/src/html/template/template.go:95 +0x35
    html/template.(*Template).Execute(0x0, 0x7f7580, 0xc0001cc000, 0x71ac40, 0x7f5300, 0xc000067290, 0x3)
            C:/tools/go/src/html/template/template.go:119 +0x36
    main.Homehandler(0x7f9f60, 0xc0001cc000, 0xc000124300)
            C:/Users/Taavetti/Documents/Go/Sources/src/Projects/VillaBrutus_Main/1_Go_Files/0_Homepagina.go:14 +0xc8
    net/http.HandlerFunc.ServeHTTP(0x7b3ca8, 0x7f9f60, 0xc0001cc000, 0xc000124300)
            C:/tools/go/src/net/http/server.go:1964 +0x4b
    net/http.(*ServeMux).ServeHTTP(0xa438e0, 0x7f9f60, 0xc0001cc000, 0xc000124300)
            C:/tools/go/src/net/http/server.go:2361 +0x12e
    net/http.serverHandler.ServeHTTP(0xc00004b040, 0x7f9f60, 0xc0001cc000, 0xc000124300)
            C:/tools/go/src/net/http/server.go:2741 +0xb2
    net/http.(*conn).serve(0xc000172140, 0x7fa120, 0xc000110440)
            C:/tools/go/src/net/http/server.go:1847 +0x64d
    created by net/http.(*Server).Serve
            C:/tools/go/src/net/http/server.go:2851 +0x2fc
    2018/11/12 10:07:12 http: panic serving [::1]:58092: runtime error: invalid memory address or nil pointer dereference
    goroutine 22 [running]:
    net/http.(*conn).serve.func1(0xc00004d040)
            C:/tools/go/src/net/http/server.go:1746 +0xd7
    panic(0x73a9c0, 0xa34b30)
            C:/tools/go/src/runtime/panic.go:513 +0x1c7
    html/template.(*Template).escape(0x0, 0x0, 0x0)
            C:/tools/go/src/html/template/template.go:95 +0x35
    html/template.(*Template).Execute(0x0, 0x7f7580, 0xc0001cc0e0, 0x71ac40, 0x7f5300, 0xc000067410, 0x3)
            C:/tools/go/src/html/template/template.go:119 +0x36
    main.Homehandler(0x7f9f60, 0xc0001cc0e0, 0xc000126200)
            C:/Users/Taavetti/Documents/Go/Sources/src/Projects/VillaBrutus_Main/1_Go_Files/0_Homepagina.go:14 +0xc8
    net/http.HandlerFunc.ServeHTTP(0x7b3ca8, 0x7f9f60, 0xc0001cc0e0, 0xc000126200)
            C:/tools/go/src/net/http/server.go:1964 +0x4b
    net/http.(*ServeMux).ServeHTTP(0xa438e0, 0x7f9f60, 0xc0001cc0e0, 0xc000126200)
            C:/tools/go/src/net/http/server.go:2361 +0x12e
    net/http.serverHandler.ServeHTTP(0xc00004b040, 0x7f9f60, 0xc0001cc0e0, 0xc000126200)
            C:/tools/go/src/net/http/server.go:2741 +0xb2
    net/http.(*conn).serve(0xc00004d040, 0x7fa120, 0xc00004e3c0)
            C:/tools/go/src/net/http/server.go:1847 +0x64d
    created by net/http.(*Server).Serve
            C:/tools/go/src/net/http/server.go:2851 +0x2fc

我仍然在尝试自己解决这个问题,因为我想表明我也在为此付出努力,而不仅仅是试图从你那里得到所有答案。但如果您能帮助我找到正确的方向,那将会很有帮助。

(另外,我是一个新用户,如果我违反了此线程中的某些规则,请在您否决此威胁后通知我,我真的很喜欢 stackoverflow,并且我正在努力了解这个社区是如何运作的。)

villabrutus_main.zip

(添加了我当前的应用程序,包括错误,也许这有帮助?,请注意有一些荷兰语评论)


解决方案


您已为 / 分配了多个处理程序。 看一下你的代码两次:

func webhost(){
    http.handle("/", 
    http.fileserver(http.dir("./html_paginas"))) 
    http.handle("/", http.fileserver(http.dir("./go_files"))) 
    http.handle("/", http.fileserver(http.dir("./storage")))
    //...
}

所以 http 在这种情况下一定要恐慌。所以解决这个问题的方法是:

func webhost(){
    http.handle("/html", 
    http.fileserver(http.dir("./html_paginas"))) 
    http.handle("/go", http.fileserver(http.dir("./go_files"))) 
    http.handle("/storage", 
    http.fileserver(http.dir("./storage")))
    //...
}

或者:

func webHost(){
    http.Handle("/", http.FileServer(http.Dir("./root"))) 
    //...
}

现在将您的 html、go、存储目录移动到 root 目录。

理论要掌握,实操不能落!以上关于《更新 1.1 在文件夹内运行 Go 文件和 JSON 文件时出现问题》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>