go - Passing around structs -
i new go , coming ruby background. trying understand code structuring in world without classes , making mistake wanting "the ruby way" in go.
i trying refactor code make more modular / readable moved loading of configuration file own package. idea?
package configuration import "github.com/burntsushi/toml" type config struct { temperatures []struct { degrees int units string } } func load() config { var cnf config _, err := toml.decodefile("config", &cnf) if err != nil { panic(err) } return cnf }
now, in main package:
package main import "./configuration" var conf configuration = configuration.load()
gives undefined: config
. understand why. copy struct definition in main package that's not dry.
it's understanding passing around structs bad practice makes code harder understand (now needs know config struct).
is hiding logic in package trying here idea in go? if so, what's "go" way pass config struct around?
in main package should specify
var conf configuration.config = configuration.load()
configuration
refers imported package , config
exported struct (uppercase name) package. can omit this, type can inferred
var conf = configuration.load()
as side note: please don't use relative imports
Comments
Post a Comment