使用 Goland IDE 编译之后,运行出错 (模板相对路径的问题)

IDE:Goland 2019.2.1
GO: 1.12.9
OS:Win10 64bit

本节的代码如果用命令行直接编译(go build)之后执行,可以正常运行

但是如果用Goland的编译工具进行编译之后,从浏览器访问/login 的之后会提示如下错误

method: GET
2019/08/30 23:42:56 http: panic serving [::1]:2693: runtime error: invalid memory address or nil pointer dereference
goroutine 6 [running]:
net/http.(*conn).serve.func1(0xc0000fa0a0)
    C:/Go/src/net/http/server.go:1769 +0x140
panic(0x767640, 0xaaaa90)
    C:/Go/src/runtime/panic.go:522 +0x1c3
html/template.(*Template).escape(0x0, 0x0, 0x0)
    C:/Go/src/html/template/template.go:95 +0x35
html/template.(*Template).Execute(0x0, 0x842720, 0xc00017c000, 0x0, 0x0, 0x842840, 0xc00010e180)
    C:/Go/src/html/template/template.go:119 +0x36
main.login(0x8485a0, 0xc00017c000, 0xc000166000)
    E:/Go/src/github.com/iwinstar1980/gowebapp/main.go:29 +0x3e7
net/http.HandlerFunc.ServeHTTP(0x7e3760, 0x8485a0, 0xc00017c000, 0xc000166000)
    C:/Go/src/net/http/server.go:1995 +0x4b
net/http.(*ServeMux).ServeHTTP(0xab9ce0, 0x8485a0, 0xc00017c000, 0xc000166000)
    C:/Go/src/net/http/server.go:2375 +0x1dd
net/http.serverHandler.ServeHTTP(0xc000058b60, 0x8485a0, 0xc00017c000, 0xc000166000)
    C:/Go/src/net/http/server.go:2774 +0xaf
net/http.(*conn).serve(0xc0000fa0a0, 0x848b60, 0xc00010c040)
    C:/Go/src/net/http/server.go:1878 +0x858
created by net/http.(*Server).Serve
    C:/Go/src/net/http/server.go:2884 +0x2fb

应该是找不到login.gtpl 这个模板导致的,因为Goland编译之后会把exe文件放到bin目录里面,这样就会找不到模板文件了

请问除了改成绝对路径之外,还有什么方法可以解决吗

讨论数量: 3

看下这个符合你的要求吗?
file

4年前 评论

这是goland默认项目路径的问题,通过goland跑代码时,修改一下路径。

file

file

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!