登录
首页 >  Golang >  Go问答

将结构体类型传递给模块的函数方式是什么?

来源:stackoverflow

时间:2024-03-28 09:00:29 322浏览 收藏

哈喽!今天心血来潮给大家带来了《将结构体类型传递给模块的函数方式是什么?》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我只是玩了一下 go 来学习如何处理。

我有一个主要的 go 程序。 我为数据库模块创建了一个子文件夹,因为我想要执行数据库操作。

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

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

这是实现目标的好方法吗? 感谢您的帮助。

./main.go

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

type configuration struct {
    apiport    string
    dbhost     string
    dbport     string
    dbuser     string
    dbpassword string
    dbname     string
    info       string
}

var appconfig = configuration{}

func readconfig() configuration {

    err := gonfig.getconf("./config.development.json", &appconfig)
    if err != nil {
        fmt.println("error: config konnte nicht geladen werden: ", err.error())
    }
    return appconfig
}

./dbapi/test.go

package dbapi

import (
    // ...
)

func Test (w http.ResponseWriter, Appconfig /* ?!? */) error {
    fmt.Println("Test: " + Appconfig.DBUser)
}

解决方案


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

struct 对象移动到 types.go

package types

type configuration struct {
    apiport    string
    dbhost     string
    dbport     string
    dbuser     string
    dbpassword string
    dbname     string
    info       string
}

现在您可以从 main.godbapi/test.go 访问它。

ma​​in.go:

package main

import "... .../code/types"

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 = types.configuration{}

func readconfig() types.configuration {

    err := gonfig.getconf("./config.development.json", &appconfig)
    if err != nil {
        fmt.println("error: config konnte nicht geladen werden: ",     err.error())
    }
    return appconfig
}

dbapi.go:

package dbapi

import "... .../code/types"

func test (w http.responsewriter, appconfig *types.configuration) error {
    fmt.println("test: " + appconfig.dbuser)
}

appconfig 位于您已在主包中声明的配置结构类型的测试函数上。由于 go 不支持循环依赖,因此您无法将 configuration 结构导入 dbapi 包中以在其中使用它。因此,我将从主包中移出 configuration 结构并在 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)
}

然后,您可以通过从 dbapi 包导入来使用 main 中的 configuration 结构。

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
}

今天关于《将结构体类型传递给模块的函数方式是什么?》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>