dua6992 2018-11-09 09:28
浏览 71

更新1.1麻烦,无法在文件夹中运行Go文件和JSON文件

Update 1.1

After receiving helpful feedback about changing the handler path I used that feedback in my application, but weirdly enough I am still receiving some error feed. I am trying to figure it out by myself, but I thought I might share it with you guys. This is also the reason why I canceled the answer that was given right now. Also some answers were to confusing for me right now to understand how that was going to work for me.

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.


}

So here the new panic (sorry for the wall of text, I wasn't sure what to leave out so I just copy pasted everything here:

   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

Again I am still trying to figure this out by myself, since I want to show I am also putting effort into this and not just trying to get all the answers out of you. But it would be helpful if you could help me into the right direction.

(Also again I am a new user, if I am breaking some rules in this thread please notify me after you've down-voted this threat, I really enjoy stackoverflow and I am trying to learn how this community works.)

VillaBrutus_Main.zip

(Added my current application, including the error, maybe that helps?, be aware there are some dutch comments)

  • 写回答

2条回答 默认 最新

  • dongsigan2636 2018-11-09 10:05
    关注

    You have assigned / with multiple handlers. Take a look at your code twice:

    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")))
        //...
    }
    

    So the http must have to panic in this situation. So the fixes for this are:

    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")))
        //...
    }
    

    Or:

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

    Now move your html, go, storage directories to the root directory.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!