无法将Context用作类型”handlerfunc” gin-gonic。

huangapple go评论63阅读模式
英文:

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

我的项目结构如下图所示:

无法将Context用作类型”handlerfunc” gin-gonic。

编辑:
添加了VS Code终端和我的终端的go env。我将它们放在屏幕截图中以避免人为错误。

VS Code终端:
无法将Context用作类型”handlerfunc” gin-gonic。

iterm2终端:
无法将Context用作类型”handlerfunc” gin-gonic。

注意:
如果我将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

无法将Context用作类型”handlerfunc” gin-gonic。

Edit
Adding go env for the terminal in VS Code and my terminal. I will put them on screenshot as to avoid human error

VS Code terminal:
无法将Context用作类型”handlerfunc” gin-gonic。

iterm2 Terminal:
无法将Context用作类型”handlerfunc” gin-gonic。

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

huangapple
  • 本文由 发表于 2016年9月24日 17:06:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/39674575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定