dongtan7639
2016-09-24 09:06
浏览 299
已采纳

不能将上下文用作“ handlerfunc” gin-gonic类型

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

enter image description here

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: enter image description here

iterm2 Terminal: enter image description here

Note The issue can be avoided, as well, if I put the HandlerFunc functions from the properties folder to main.go file.

图片转代码服务由CSDN问答提供 功能建议

这已经困扰了我很长时间了。 我正在使用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/6edadf1dedc63b4b4b4e231891cae8e9a5

我的项目结构如下

“在此处输入图像描述”

编辑 在VS Code和我的终端中为终端添加 go env 。 我将它们放在屏幕快照上,以避免人为错误

VS代码终端: “在此处输入图片说明”

iterm2终端: ”在此处输入图片描述“

注意 也可以避免此问题,如果我把 从属性文件夹到main.go文件的HandlerFunc函数。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongqi1245 2016-09-25 13:17
    已采纳

    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.

    已采纳该答案
    打赏 评论
  • dongluni0568 2019-07-18 13:25
    go get -u github.com/gin-gonic/gin
    govendor add +e
    
    打赏 评论

相关推荐 更多相似问题