dongshanfan1941 2019-02-26 05:33
浏览 2031
已采纳

在Golang中如何在其他包文件中使用struct?

I am new in Golang and need some help.

As you can see in the code below I am tring to create REST API in Golang. I use mux (Gorilla Mux) and pq (PostgreSQL driver) as third party libraries. Don't want to use ORM.

Inside application.go file I have InitializeRoutes function with a list of all aviable routes. GetFactors function process one of these routes. I am tring to define GetFactors function logic in other file called factors.go. Inside factors.go file I want to use Application struct which was defined in application.go. How to make it correctly? Right now as you can see they are in different packages. For thats why factors.go file don't see Application struct.

Project structure:

main.go
application.go
controllers
    factors.go

main.go:

package main

func main()  {
    application := Application{}
    application.Initialization()
    application.Run("localhost:8000")
}

application.go:

package main

import (
    "database/sql"
    "github.com/gorilla/mux"
    "log"
    "net/http"
    "rest-api/configurations"
)

type Application struct {
    Router *mux.Router
    Database *sql.DB
}

func (application *Application) Initialization() {
    var err error
    application.Database, err = configurations.DatabaseConnection()
    if err != nil {
        log.Fatal(err)
    }

    application.Router = mux.NewRouter()
    application.Router.StrictSlash(true)

    application.InitializeRoutes()
}

func (application *Application) Run(address string) {
    log.Fatal(http.ListenAndServe(address, application.Router))
}

func (application *Application) InitializeRoutes() {
    application.Router.HandleFunc("/api/factors", application.GetFactors).Methods("GET")
    // other code
}

controllers/factors.go:

package controllers

import (
    "net/http"
)

func (application *Application) GetFactors(rw http.ResponseWriter, request *http.Request) {
    // code
}
  • 写回答

1条回答 默认 最新

  • duanjue7508 2019-02-26 10:57
    关注

    Well, finally I decided to redesign the project structure.

    main.go
    routes
        routes.go
    controllers
        factors.go
    models
        factors.go
    

    main.go:

    import (
        "your_project_name/routes"
    )
    
    func main()  {
        // code
        router := mux.NewRouter()
        routes.Use(router)
        // code
    }
    

    routes/routes.go:

    package routes
    
    import (
        "github.com/gorilla/mux"
        "your_application_name/controllers"
    )
    
    func Use(router *mux.Router) {
        router.HandleFunc("/api/factors", controllers.GetFactors).Methods("GET")
    }
    

    controllers/factors.go:

    package controllers
    
    var GetFactors = func(res http.ResponseWriter, req *http.Request) {
        // code
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格