将结构体传递给跨包的函数

huangapple go评论76阅读模式
英文:

Passing structs to functions across packages

问题

为了更熟悉Go语言,我正在尝试重构一些已经正常工作的代码。

原始代码有三个结构体:

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
}

这些结构体被传递给一个函数,如下所示:

func runpinger(clientStatsd statsd.Statter, defaults *ConfigDefaults, group *ConfigGroup) {
    // 一些代码
}

现在,我已经重新设计了配置(使用gocfg),改用hcl,它提供了更清晰的配置语法。

我将配置解析器移到了一个名为config的包中,其中的结构体如下所示:

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"`
}

然后,在config包中有一个函数,如下所示:

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

}

现在,在我的主包中,我想再次将这些结构体传递给函数。如何在不同的包之间实现这个?

我尝试过:

func(runpinger config *config.Config) {
    // 这里
}

但似乎不起作用。理想情况下,我希望只传递一个指向“子结构体”(即TargetGroups结构体)的指针,尽管我不确定是否可能实现这一点。

英文:

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.

答案1

得分: 1

你应该能够将结构体传递给主包,只需确保在文件的顶部放置import "path/to/config"

路径必须是从$GOPATH/src/目录到你的包的完整路径。

英文:

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

huangapple
  • 本文由 发表于 2017年1月17日 06:01:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/41685713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定