Go 如何将结构类型传递给模块函数?

Go 如何将结构类型传递给模块函数?,go,Go,我只是和Go玩一点来学习操控 我有一个主要的围棋项目。 我为一个数据库模块创建了一个子文件夹,我想在其中执行数据库操作 对于参数,我有一个带有凭证等的结构类型。 如何将结构传递给模块 我需要配置结构中所需的所有配置和全局变量,并在需要时将其传递给模块 这是实现目标的好方法吗? 谢谢你的帮助 /main.go: 主程序包 进口( //... “github.com/wyrnixx/go-server/src/go-server/dbapi” ) func handlerTest(w http.Re

我只是和Go玩一点来学习操控

我有一个主要的围棋项目。 我为一个数据库模块创建了一个子文件夹,我想在其中执行数据库操作

对于参数,我有一个带有凭证等的结构类型。 如何将结构传递给模块

我需要配置结构中所需的所有配置和全局变量,并在需要时将其传递给模块

这是实现目标的好方法吗? 谢谢你的帮助

/main.go

主程序包
进口(
//...
“github.com/wyrnixx/go-server/src/go-server/dbapi”
)
func handlerTest(w http.ResponseWriter,r*http.Request){
log.Println(“请求了信息:/test…”)
dbapi.Test(&AppConfig)
}
func main(){
http.HandleFunc(“/test”,handlerTest)
AppConfig=ReadConfig()
log.Fatal(http.ListenAndServe(AppConfig.ApiPort,nil))
}
类型配置结构{
端口串
数据库主机字符串
数据端口字符串
数据库用户字符串
DBPassword字符串
数据库名字符串
信息字符串
}
var AppConfig=配置{}
func ReadConfig()配置{
err:=gonfig.GetConf(“./config.development.json”、&AppConfig)
如果错误!=零{
Println(“错误:Config-konnte-nicht-geladen-werden:,err.ERROR())
}
返回AppConfig
}
/dbapi/test.go

包dbapi
进口(
// ...
)
func测试(带http.ResponseWriter,Appconfig/*?!?*/)错误{
fmt.Println(“测试:”+Appconfig.DBUser)
}

Configuration struct类型的测试函数上的AppConfig,该函数已在主程序包中声明。由于go不支持循环依赖关系,因此无法将配置结构导入dbapi包中以在其中使用它。因此,我将从主包中移动Configuration struct,并在dbapi包中声明它

package dbapi

import (...)

type Configuration struct {
    ApiPort    string
    DBHost     string
    DBPort     string
    DBUser     string
    DBPassword string
    DBName     string
    Info       string
}


func Test (w http.ResponseWriter, Appconfig *Configuration) error {
    fmt.Println("Test: " + Appconfig.DBUser)
}
var AppConfig = dbapi.Configuration{}
然后,通过从dbapi包导入配置结构,可以在main中使用它

package dbapi

import (...)

type Configuration struct {
    ApiPort    string
    DBHost     string
    DBPort     string
    DBUser     string
    DBPassword string
    DBName     string
    Info       string
}


func Test (w http.ResponseWriter, Appconfig *Configuration) error {
    fmt.Println("Test: " + Appconfig.DBUser)
}
var AppConfig = dbapi.Configuration{}
因此,您的主程序包如下所示:

package main

import(
...
"github.com/wyrdnixx/go-server/src/go-server/dbapi"
)

func handlerTest(w http.ResponseWriter, r *http.Request) {
    log.Println("INFO: /test was requested...")
    dbapi.Test(&AppConfig)
}
func main() {
    http.HandleFunc("/test", handlerTest)
    AppConfig = ReadConfig()
    log.Fatal(http.ListenAndServe(AppConfig.ApiPort, nil))  
}

var AppConfig = dbapi.Configuration{}

func ReadConfig() dbapi.Configuration {

    err := gonfig.GetConf("./config.development.json", &AppConfig)
    if err != nil {
        fmt.Println("ERROR: Config konnte nicht geladen werden: ",     err.Error())
    } 
    return dbapi.AppConfig
}

在主包中声明的Configuration struct类型的测试函数上的AppConfig。由于go不支持循环依赖关系,因此无法将配置结构导入dbapi包中以在其中使用它。因此,我将从主包中移动Configuration struct,并在dbapi包中声明它

package dbapi

import (...)

type Configuration struct {
    ApiPort    string
    DBHost     string
    DBPort     string
    DBUser     string
    DBPassword string
    DBName     string
    Info       string
}


func Test (w http.ResponseWriter, Appconfig *Configuration) error {
    fmt.Println("Test: " + Appconfig.DBUser)
}
var AppConfig = dbapi.Configuration{}
然后,通过从dbapi包导入配置结构,可以在main中使用它

package dbapi

import (...)

type Configuration struct {
    ApiPort    string
    DBHost     string
    DBPort     string
    DBUser     string
    DBPassword string
    DBName     string
    Info       string
}


func Test (w http.ResponseWriter, Appconfig *Configuration) error {
    fmt.Println("Test: " + Appconfig.DBUser)
}
var AppConfig = dbapi.Configuration{}
因此,您的主程序包如下所示:

package main

import(
...
"github.com/wyrdnixx/go-server/src/go-server/dbapi"
)

func handlerTest(w http.ResponseWriter, r *http.Request) {
    log.Println("INFO: /test was requested...")
    dbapi.Test(&AppConfig)
}
func main() {
    http.HandleFunc("/test", handlerTest)
    AppConfig = ReadConfig()
    log.Fatal(http.ListenAndServe(AppConfig.ApiPort, nil))  
}

var AppConfig = dbapi.Configuration{}

func ReadConfig() dbapi.Configuration {

    err := gonfig.GetConf("./config.development.json", &AppConfig)
    if err != nil {
        fmt.Println("ERROR: Config konnte nicht geladen werden: ",     err.Error())
    } 
    return dbapi.AppConfig
}

在主目录下创建另一个包,将其命名为
types
或任何您想要的名称。像下面这样:

结构
对象移动到
类型。转到

包类型
类型配置结构{
端口串
数据库主机字符串
数据端口字符串
数据库用户字符串
DBPassword字符串
数据库名字符串
信息字符串
}
现在,您可以从
main.go
dbapi/test.go
访问它

main.go:

主程序包
导入“…../code/types”
func handlerTest(w http.ResponseWriter,r*http.Request){
log.Println(“请求了信息:/test…”)
dbapi.Test(&AppConfig)
}
func main(){
http.HandleFunc(“/test”,handlerTest)
AppConfig=ReadConfig()
log.Fatal(http.ListenAndServe(AppConfig.ApiPort,nil))
}
var AppConfig=types.Configuration{}
func ReadConfig()类型。配置{
err:=gonfig.GetConf(“./config.development.json”、&AppConfig)
如果错误!=零{
Println(“错误:Config-konnte-nicht-geladen-werden:,err.ERROR())
}
返回AppConfig
}
dbapi.go:

包dbapi
导入“…../code/types”
func测试(带http.ResponseWriter,Appconfig*types.Configuration)错误{
fmt.Println(“测试:”+Appconfig.DBUser)
}

在主目录下创建另一个包,将其命名为
类型
或任何您想要的名称。像下面这样:

结构
对象移动到
类型。转到

包类型
类型配置结构{
端口串
数据库主机字符串
数据端口字符串
数据库用户字符串
DBPassword字符串
数据库名字符串
信息字符串
}
现在,您可以从
main.go
dbapi/test.go
访问它

main.go:

主程序包
导入“…../code/types”
func handlerTest(w http.ResponseWriter,r*http.Request){
log.Println(“请求了信息:/test…”)
dbapi.Test(&AppConfig)
}
func main(){
http.HandleFunc(“/test”,handlerTest)
AppConfig=ReadConfig()
log.Fatal(http.ListenAndServe(AppConfig.ApiPort,nil))
}
var AppConfig=types.Configuration{}
func ReadConfig()类型。配置{
err:=gonfig.GetConf(“./config.development.json”、&AppConfig)
如果错误!=零{
Println(“错误:Config-konnte-nicht-geladen-werden:,err.ERROR())
}
返回AppConfig
}
dbapi.go:

包dbapi
导入“…../code/types”
func测试(带http.ResponseWriter,Appconfig*types.Configuration)错误{
fmt.Println(“测试:”+Appconfig.DBUser)
}

我猜您在
func ReadConfig()dbapi.Configuration
中缺少了
dbapi
,我不明白您的意思?配置在主包中读取,并传递给dbapi包中的测试函数。@Mania查看主包中的
ReadConfig()
函数的输出。您不能直接访问
配置
结构。我想您在
func ReadConfig()dbapi中缺少了
dbapi
。配置
我不明白您的意思?配置在主包中读取,并传递给dbapi包中的测试函数。@Mania查看中的
ReadConfig()
函数的输出