douda5706
2016-08-15 18:32
浏览 21
已采纳

引用另一个结构给出“未定义”

I have some very simple golang code:

func main (){

      type config struct {
        interval     int    `mapstructure:"Interval"`
        statsdPrefix string `mapstructure:"statsd_prefix"`
        groups       []group
      }

      type group struct {
        group        string `mapstructure:"group"`
        targetPrefix string `mapstructure:"target_prefix"`
        targets      []target
      }
}

When I run this, I get the following:

undefined: group

What am I missing here?

图片转代码服务由CSDN问答提供 功能建议

我有一些非常简单的golang代码:

  func main  (){
 
类型配置结构{
 interval int`mapstructure:“ Interval”`
 statsdPrefix string`mapstructure:“ statsd_prefix”`
 groups [] group 
} 
 
类型group struct {  
组字符串`mapstructure:“ group”`
 targetPrefix字符串`mapstructure:“ target_prefix”`
目标[] target 
} 
} 
   
 
 

运行此命令时,我得到以下信息:

未定义:组

我在这里缺少什么?< / p>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doulang6695 2016-08-15 18:36
    已采纳

    Since you've defined the types in a function, the config type definition runs before there is a group type to reference. Reversing the order of your definitions works, although I had to remove the reference to target as you have not provided it's definition.

    This works in the playgound https://play.golang.org/p/fzRCtCHqnH:

    func main() {
    
        type group struct {
            group        string `mapstructure:"group"`
            targetPrefix string `mapstructure:"target_prefix"`
        }
        type config struct {
            interval     int    `mapstructure:"Interval"`
            statsdPrefix string `mapstructure:"statsd_prefix"`
            groups       []group
        }
    
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题