duanlongling5308
duanlongling5308
2015-06-27 21:30
采纳率: 0%
浏览 53
已采纳

将Go代码拆分到多个文件时遇到问题

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

package main

import (
    "github.com/gin-gonic/gin"
    "net/http"
)

func main() {
    // Creates a gin router with default middlewares:
    // logger and recovery (crash-free) middlewares
    router := gin.Default()

    v1 := router.Group("/v1")
    {
        v1.GET("/", func (c *gin.Context) {
            c.JSON(http.StatusOK, "{'sup': 'dup'}")
        })

        groups := v1.Group("/groups")
        {
            groups.GET("/", groupIndex)

            groups.GET("/:id", groupShow)
            groups.POST("/", groupCreate)
            groups.PUT("/:id", groupUpdate)
            groups.DELETE("/:id", groupDelete)
        }
    }

    // Listen and server on 0.0.0.0:8080
    router.Run(":3000")
}

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

package main

import (
  "strings"
  "github.com/gin-gonic/gin"
)


func groupIndex(c *gin.Context) {
  var group struct {
    Name string
    Description string
  }

  group.Name = "Famzz"
  group.Description = "Jamzzz"

  c.JSON(http.StatusOK, group)
}

func groupShow(c *gin.Context) {
  c.JSON(http.StatusOK, "{'groupShow': 'someContent'}")
}

func groupCreate(c *gin.Context) {
  c.JSON(http.StatusOK, "{'groupShow': 'someContent'}")
}

func groupUpdate(c *gin.Context) {
  c.JSON(http.StatusOK, "{'groupUpdate': 'someContent'}")
}

func groupDelete(c *gin.Context) {
  c.JSON(http.StatusOK, "{'groupDelete': 'someContent'}")
}

But when I try to compile I get the following error

stuff/main.go:21: undefined: groupIndex
stuff/main.go:23: undefined: groupShow
stuff/main.go:24: undefined: groupCreate
stuff/main.go:25: undefined: groupUpdate
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?

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

我有两个文件 main.go group.go ...看起来像这样

 包main 
 
import(
“ github.com/gin-gonic/gin"
” net / http“  
)
 
func main(){
 //使用默认中间件创建gin路由器:
 //记录器和恢复(无崩溃)中间件
 router:= gin.Default()
 
  v1:= router.Group(“ / v1”)
 {
 v1.GET(“ /”,func(c * gin.Context){
 c.JSON(http.StatusOK,“ {'sup':  'dup'}“)
})
 
组:= v1.Group(” / groups“)
 {
 groups.GET(” /“,groupIndex)
 
 groups.GET(”  /:id“,groupShow)
 groups.POST(” /“,groupCreate)
 groups.PUT(” /:id“,groupUpdate)
 groups.DELETE(” /:id“,groupDelete)
}  
} 
 
 //在0.0.0.0:8080
路由器上监听服务器并运行。Run(“:3000”)
} 
   
 
 

方法 groupIndex groupCreate groupUpdate 等位于 routes / group.go

  package main 
 
import(
  “ strings” 
“ github.com/gin-gonic/gin"
)


func groupIndex(c * gin.Context){
 var group struct {
 Name string 
 Description string 
  } 
 
 group.Name =“ Famzz” 
 group.Description =“ Jamzzz” 
 
 c.JSON(http.StatusOK,group)
} 
 
func groupShow(c * gin.Context)  {
 c.JSON(http.StatusOK,“ {'groupShow':'someContent'}”)
} 
 
func groupCreate(c * gin.Context){
 c.JSON(http.StatusOK,“  {'groupShow':'someContent'}“)
} 
 
func groupUpdate(c * gin.Context){
 c.JSON(http.StatusOK,” {'groupUpdate':'someContent'}“)\\  n} 
 
func groupDelete(c * gin.Context){
 c.JSON(http.StatusOK,“ {'groupDelete':'someContent'}”)
} 
    
 
 

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

  stuff / main.go:21:未定义:groupIndex 
stuff / main.go:  23:未定义:groupShow 
stuff /main。24:未定义:groupCreate 
stuff / main。 转到:25:未定义:groupUpdate 
stuff / main。转到:26:未定义:groupDelete 
   
 
 

我是新手,但是我想如果你把 文件放在同一软件包中,则它们将可以相互访问。 我在这里做错了什么?

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

1条回答 默认 最新

  • duan0821
    duan0821 2015-06-27 21:40
    已采纳

    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:

    func GroupIndex(c *gin.Context) {
    

    Import the package from main:

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

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

    点赞 评论

相关推荐