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

在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
}

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

我是 Golang 的新手,需要帮助。 \ n

正如您在下面的代码中看到的那样,我正努力在Golang中创建REST API。 我使用 mux (大猩猩Mux)和 pq (PostgreSQL驱动程序)作为第三方库。 不想使用ORM。

内部 application.go 文件中,我具有 InitializeRoutes 函数,其中包含所有可用路由的列表。 GetFactors 函数处理这些路由之一。 我正在尝试在名为 factors.go 的其他文件中定义 GetFactors 函数逻辑。 在 factors.go 文件中,我想使用在 application.go 中定义的 Application 结构。 如何正确制作? 现在,您可以看到它们位于不同的软件包中。 因此,为什么 factors.go 文件没有看到 Application 结构。

项目结构:< /p>

main.go
application.go
controllers
 factor.go 
   
 
 

main.go :

 包main 
 
func main(){
 application:= Application {} 
 application.Initialization()
 application.Run(  “本地主机:8000”)\ N} 
   
 
 

<强> application.go

 <代码 >软件包main 
 
import(
“数据库/sql”
“github.com/gorilla/mux”
“日志” 
“ net / http” 
“ rest-api / configurations” 
)  
 
type应用程序结构{
路由器* mux.Router 
数据库* sql.DB 
} 
 
func(应用程序* Application)Initialization(){
 var err error 
 application.Database,err =  configuration.DatabaseConnection()
如果err!= nil {
 log.Fatal(err)
} 
 
 application.Router = mux.NewRouter()
 application.Router.StrictSlash(true)
 \  n应用程序 lizeRoutes()
} 
 
func(应用程序* Application)运行(地址字符串){
 log.Fatal(http.ListenAndServe(地址,application.Router))
} 
 
func(应用程序* Application)  InitializeRoutes(){
 application.Router.HandleFunc(“ / api / factors”,application.GetFactors).Methods(“ GET”)
 //其他代码
} 
   \  n 
 

controllers / factors.go

 程序包控制器
 
import(
“ net / http” 
)  
 
func(应用程序* Application)GetFactors(rw http.ResponseWriter,请求* http.Request){
 //代码
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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
    }
    
    点赞 评论

相关推荐 更多相似问题