我遇到了将Go代码拆分为多个文件的问题。

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

Having trouble splitting go code in multiple files

问题

我有两个文件main.gogroup.go...大致如下所示:

  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. )
  6. func main() {
  7. // 创建一个带有默认中间件(logger和recovery)的gin路由器
  8. router := gin.Default()
  9. v1 := router.Group("/v1")
  10. {
  11. v1.GET("/", func(c *gin.Context) {
  12. c.JSON(http.StatusOK, "{'sup': 'dup'}")
  13. })
  14. groups := v1.Group("/groups")
  15. {
  16. groups.GET("/", groupIndex)
  17. groups.GET("/:id", groupShow)
  18. groups.POST("/", groupCreate)
  19. groups.PUT("/:id", groupUpdate)
  20. groups.DELETE("/:id", groupDelete)
  21. }
  22. }
  23. // 监听并在0.0.0.0:8080上提供服务
  24. router.Run(":3000")
  25. }

所以groupIndexgroupCreategroupUpdate等方法位于routes/group.go文件中:

  1. package main
  2. import (
  3. "strings"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func groupIndex(c *gin.Context) {
  7. var group struct {
  8. Name string
  9. Description string
  10. }
  11. group.Name = "Famzz"
  12. group.Description = "Jamzzz"
  13. c.JSON(http.StatusOK, group)
  14. }
  15. func groupShow(c *gin.Context) {
  16. c.JSON(http.StatusOK, "{'groupShow': 'someContent'}")
  17. }
  18. func groupCreate(c *gin.Context) {
  19. c.JSON(http.StatusOK, "{'groupShow': 'someContent'}")
  20. }
  21. func groupUpdate(c *gin.Context) {
  22. c.JSON(http.StatusOK, "{'groupUpdate': 'someContent'}")
  23. }
  24. func groupDelete(c *gin.Context) {
  25. c.JSON(http.StatusOK, "{'groupDelete': 'someContent'}")
  26. }

但是当我尝试编译时,出现以下错误:

  1. stuff/main.go:21: undefined: groupIndex
  2. stuff/main.go:23: undefined: groupShow
  3. stuff/main.go:24: undefined: groupCreate
  4. stuff/main.go:25: undefined: groupUpdate
  5. stuff/main.go:26: undefined: groupDelete

我对Go语言非常陌生,但我认为如果将文件放在同一个包中,它们应该可以相互访问。我在这里做错了什么?

英文:

I have two files main.go and group.go... it looks something like this

  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. )
  6. func main() {
  7. // Creates a gin router with default middlewares:
  8. // logger and recovery (crash-free) middlewares
  9. router := gin.Default()
  10. v1 := router.Group("/v1")
  11. {
  12. v1.GET("/", func (c *gin.Context) {
  13. c.JSON(http.StatusOK, "{'sup': 'dup'}")
  14. })
  15. groups := v1.Group("/groups")
  16. {
  17. groups.GET("/", groupIndex)
  18. groups.GET("/:id", groupShow)
  19. groups.POST("/", groupCreate)
  20. groups.PUT("/:id", groupUpdate)
  21. groups.DELETE("/:id", groupDelete)
  22. }
  23. }
  24. // Listen and server on 0.0.0.0:8080
  25. router.Run(":3000")
  26. }

So the methods groupIndex, groupCreate, groupUpdate, etc are located in another file under routes/group.go

  1. package main
  2. import (
  3. "strings"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func groupIndex(c *gin.Context) {
  7. var group struct {
  8. Name string
  9. Description string
  10. }
  11. group.Name = "Famzz"
  12. group.Description = "Jamzzz"
  13. c.JSON(http.StatusOK, group)
  14. }
  15. func groupShow(c *gin.Context) {
  16. c.JSON(http.StatusOK, "{'groupShow': 'someContent'}")
  17. }
  18. func groupCreate(c *gin.Context) {
  19. c.JSON(http.StatusOK, "{'groupShow': 'someContent'}")
  20. }
  21. func groupUpdate(c *gin.Context) {
  22. c.JSON(http.StatusOK, "{'groupUpdate': 'someContent'}")
  23. }
  24. func groupDelete(c *gin.Context) {
  25. c.JSON(http.StatusOK, "{'groupDelete': 'someContent'}")
  26. }

But when I try to compile I get the following error

  1. stuff/main.go:21: undefined: groupIndex
  2. stuff/main.go:23: undefined: groupShow
  3. stuff/main.go:24: undefined: groupCreate
  4. stuff/main.go:25: undefined: groupUpdate
  5. stuff/main.go:26: undefined: groupDelete

I'm super new to go, but I thought if you put files in the same package, then they'll have access to each other. What am I doing wrong here?

答案1

得分: 3

有两种方法可以修复这个问题:

  1. 将group.go移动到与main.go相同的目录中。

  2. 将group.go作为一个包导入。将group.go中的包声明改为:

    package routes // 或者你选择的名称

通过以大写字母开头来导出函数:

  1. func GroupIndex(c *gin.Context) {

从main中导入该包:

  1. import "path/to/routes"
  2. ...
  3. groups.GET("/", routes.GroupIndex)

文档如何编写Go代码对此进行了更详细的解释。

英文:

There are two ways to fix this:

  1. Move group.go to the same directory as main.go.

  2. Import group.go as a package. Change the package declaration on group.go to:

    package routes // or the name of your choice

Export the functions by starting them with a capital letter:

  1. func GroupIndex(c *gin.Context) {

Import the package from main:

  1. import "path/to/routes"
  2. ...
  3. groups.GET("/", routes.GroupIndex)

The document How To Write Go Code explains this and more.

huangapple
  • 本文由 发表于 2015年6月28日 05:30:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/31093706.html
匿名

发表评论

匿名网友

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

确定