There is such an application structure

./ ├── conf │  └── conf.go ├── helpers │  └── helpers.go ├── config.cfg └── main.go 

in main.go:

 import ( "./conf" "./helpers" "fmt" ... ) var cfg conf.Config func init() { cfg = make(conf.Config) cfg.Parse("config.cfg") } func main () { fmt.Print(cfg["variable"]) } 

In /helpers/helpers.go I want to use cfg :

 func Helper() { fmt.Print(cfg["variable"]) } 

But when compiling I get the expected error helpers/helpers.go:10: undefined: cfg . How can I access cfg in helpers / helpers.go?

    3 answers 3

    You cannot import names from the main package.

    The variable cfg should be in one of the library packages, for example, conf , then it can be accessed as conf.Cfg .

    Also imported names must begin with a capital letter, i.e. Cfg .

    • So, do I need to import conf into helpers? And in general in each package, where I want to access the config? - Boris
    • Yes, you need to import it. - Abyx
    • it is sad. That is, there is no "down" area of ​​visibility? - Boris
    • No, there is nothing like that. Each external name must have its package name, regardless of where the package is located. There is no connection between subfolders. - Abyx

    Out of position as follows:

    conf / conf.go:

     type ConfigType map[string]string var Config ConfigType 

    main.go:

     //var cfg conf.Config func init() { conf.Config = make(conf.ConfigType) conf.Config.Parse("config.cfg") } func main () { fmt.Print(conf.Config["variable"]) } 

    helpers / helpers.go:

     import "../conf" func Helper() { fmt.Print(conf.Config["variable"]) } 

    It seems like it works. But - how good is this solution? Surely there is an alternative.

      You have the wrong approach.

      All package files must be at the same nesting level (in the same folder), creating a subfolder means that it is a completely different package.

       ./ ├── conf.go ├── helpers.go ├── config.cfg └── main.go 

      Using this structure, all variables (exported or not) will be visible in all package files.

      If you really want to create a package for configuring your program and a package of helpers, then create subfolders, but it would be more correct to call them plugins / drivers and when importing, they themselves should be registered via init, and then choose the main one from those available.

      conf / conf.go:

       type ConfigType map[string]string type Config{ conf ConfigType } func init() { conf := make(conf.ConfigType) // Заполнить conf по вкусу driver.Register("mysql", &driver.Driver{ Config: conf, }) } 

      main.go:

       var ( drivers = make(map[string]driver.Driver) ) func UseConfig(driverName) conf.Config { return drivers[driverName].Config } func main () { conf := UseConfig("mysql") fmt.Print(conf["variable"]) } 

      Sample driver , for example, for sql.

      • if you keep everything in one directory - how then can you import all these packages into main.go? - Boris
      • if everything is in one directory, then they do not need to be imported either into main.go, or into any other file in this directory, you can immediately use it, just refer by name. Tests, for example, if you create the file main_test.go, then everything that is available in main.go and vice versa will be available in it. - Darigaaz
      • ls test/ main.go mytype.go in mytype.go : package mytype type TT struct {..some..} in main.go : var mt mytype.TT ./main.go: undefined: mytype in mytype.TT in main.go : var mt TT ./main.go: undefined: TT - Boris
      • figured out like - Boris