dtxpz8785
2018-08-14 10:20
浏览 66
已采纳

在函数中返回struct的值,并在另一个包中使用它

I try create package config in my example project but something doesn't work as I expected, I have folder structure:

config/config.go // package config
main.go  // package main

and I want use config in my main file:

func main() {
    conf := config.GetConf()

    db := dbConn{
        schemas:  map[string]*sql.DB{},
        url:      fmt.Sprintf("tcp(%s)", conf.db['dev']),
        username: db.user,
        password: db.password,
    }
    db.create()
}

my config file:

type Config struct {
    db         map[string]string
    user     string
    password string
}

func GetConf() *Config {
    config := Config{
        db: map[string]string{
            "dev": "database.url",
        },
        user: "root",
        password: "pass",
    }

    return &config
}

compiler return error: conf.db undefined (cannot refer to unexported field or method db)

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题