跨包将结构传递给函数

In an attempt to become more familiar with go, I am trying to refactor some code which is already working fine.

The original code has three structs:

type ConfigGroup struct {
        Interval int
        Tprefix string
        Target []string
}

type ConfigDefaults struct {
        Interval int
        Sprefix string
}

type Config struct {
    Group map[string]*ConfigGroup
    Defaults ConfigDefaults
}

These structs get passed to a function like so:

func runpinger(clientStatsd statsd.Statter, defaults *ConfigDefaults, group *ConfigGroup) {
// some stuff here
}

Now, I've reworked the config (which uses gocfg) to use hcl instead, which seems to provide a cleaner config syntax.

I've moved the config parser into a package, config, with structs that look like this:

type Config struct {
  Interval int            `hcl:"interval"`
  Prefix   string         `hcl:"prefix"`
  Groups   []TargetGroups `hcl:"target_group"`
}

type TargetGroups struct {
  Name     string    `hcl:",key"`
  Prefix   string    `hcl:"prefix"`
  Interval int       `hcl:"interval"`
  Targets  []Targets `hcl:"target"`
}

type Targets struct {
  Address string `hcl:"address"`
  Label   string `hcl:"label"`
}

and then a function in config package that looks like this:

func Parse(ConfigFile string) (*Config, error) {

  result := &Config{}
  var errors *multierror.Error

  config, err := ioutil.ReadFile(ConfigFile)

  if err != nil {
    return nil, err
  }

  hclParseTree, err := hcl.Parse(string(config))
  if err != nil {
    return nil, err
  }

  if err := hcl.DecodeObject(&result, hclParseTree); err != nil {
    return nil, err
  }

  return result, errors.ErrorOrNil()

}

Now, in my main package I'd like to pass these structs to the function again. How can I do this across packages?

I tried:

func(runpinger config *config.Config) {
 // here
}

But that didn't seem to work. Ideally, I'd like to just pass a pointer to the "sub-struct" (ie the TargetGroups struct) as well, although I'm not sure if that's possible.

You should be able to pass the structs to the main package, just check that you put import "path/to/config" at the top of your file.

The path has to be the full path to your package from your $GOPATH/src/ directory