英文:
cannot use Context as type "handlerfunc" gin-gonic
问题
这个问题困扰我很久了。我正在使用gin-gonic,每次我尝试使用go run main.go
时,都会出现这个编译错误:
无法将properties.Pong(类型
func(*"github.com/carlqt/geodude/vendor/github.com/gin-gonic/gin".Context)
)作为类型"github.com/gin-gonic/gin".HandlerFunc
的参数传递给router.RouterGroup.GET
但是当我在Visual Studio Code终端中运行go run
时,它可以正常工作。
这是main.go文件的链接:https://gist.github.com/carlqt/c5f6b0d127c1ca124cf2fe8d620976a1
这是property.go文件的链接:https://gist.github.com/carlqt/6edadf1dedc63b4b4e231891cae8e9a5
我的项目结构如下图所示:
编辑:
添加了VS Code终端和我的终端的go env
。我将它们放在屏幕截图中以避免人为错误。
注意:
如果我将properties文件夹中的HandlerFunc函数放到main.go文件中,问题也可以避免。
英文:
This has been bugging me for a long time. I'm using gin-gonic and everytime I try and use go run main.go
, this compile error always appears:
> cannot use properties.Pong (type
> func(*"github.com/carlqt/geodude/vendor/github.com/gin-gonic/gin".Context)) as type "github.com/gin-gonic/gin".HandlerFunc in argument to
> router.RouterGroup.GET
But when I use Visual Studio Code terminal and run the go run
, it works.
here's the main.go file https://gist.github.com/carlqt/c5f6b0d127c1ca124cf2fe8d620976a1
And here's the property.go file: https://gist.github.com/carlqt/6edadf1dedc63b4b4e231891cae8e9a5
My project structure looks like this
Edit
Adding go env
for the terminal in VS Code and my terminal. I will put them on screenshot as to avoid human error
Note
The issue can be avoided, as well, if I put the HandlerFunc functions from the properties folder to main.go file.
答案1
得分: 1
好的,抱歉造成了这么多麻烦,但对我来说有效的方法是卸载 Go
并重新安装。之前,Go
是通过 Homebrew 安装在我的机器上的。当我重新安装时,我使用了谷歌的 Mac 安装程序。现在一切都恢复正常了。
谢谢你的帮助。
英文:
OK, sorry about all this mess but what worked for me is to uninstall Go
and reinstall it again. Previously, Go
was installed on my machine using Homebrew. When I reinstalled it, I used google's mac installer. Now all was fine again.
Thanks for helping.
答案2
得分: 0
以下是要翻译的内容:
go get -u github.com/gin-gonic/gin
govendor add +e
翻译结果:
运行以下命令进行安装:
go get -u github.com/gin-gonic/gin
使用以下命令添加依赖:
govendor add +e
英文:
go get -u github.com/gin-gonic/gin
govendor add +e
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论