dongzao4503 2018-06-26 13:59
浏览 69
已采纳

在Go中追加结构片

I have two structs, like so:

// init a struct for a single item
type Cluster struct {
  Name string
  Path string
}

// init a grouping struct
type Clusters struct {
  Cluster []Cluster
}

What I want to do is append to new items to the clusters struct. So I wrote a method, like so:

func (c *Clusters) AddItem(item Cluster) []Cluster {
  c.Cluster = append(c.Cluster, item)
  return c.Cluster
}

The way my app works, I loop through some directories then append the name of the final directory and it's path. I have a function, that is called:

func getClusters(searchDir string) Clusters {

  fileList := make([]string, 0)
  //clusterName := make([]string, 0)
  //pathName := make([]string, 0)

  e := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error {
    fileList = append(fileList, path)
    return err
  })

  if e != nil {
    log.Fatal("Error building cluster list: ", e)
  }

  for _, file := range fileList {

    splitFile := strings.Split(file, "/")
    // get the filename
    fileName := splitFile[len(splitFile)-1]

    if fileName == "cluster.jsonnet" {
      entry := Cluster{Name: splitFile[len(splitFile)-2], Path: strings.Join(splitFile[:len(splitFile)-1], "/")}
      c.AddItem(entry)

    }
  }
  Cluster := []Cluster{}
  c := Clusters{Cluster}

  return c

}

The problem here is that I don't know the correct way to do this.

Currently, I'm getting:

cmd/directories.go:41:4: undefined: c

So I tried moving this:

Cluster := []Cluster{}
c := Clusters{Cluster}

Above the for loop - range. The error I get is:

cmd/directories.go:43:20: Cluster is not a type

What am I doing wrong here?

  • 写回答

2条回答 默认 最新

  • douqian9729 2018-06-26 14:03
    关注

    The error is in the loop where you are calling AddItem function on Cluster method receiver which is not defined inside getClusters function. Define Cluster struct before for loop and then call the function c.AddItem as defined below:

    func getClusters(searchDir string) Clusters {
    
        fileList := make([]string, 0)
        fileList = append(fileList, "f1", "f2", "f3")
    
        ClusterData := []Cluster{}
        c := Clusters{Cluster: ClusterData} // change the struct name passed to Clusters struct
    
        for _, file := range fileList {
    
            entry := Cluster{Name: "name" + file, Path: "path" + file}
            c.AddItem(entry)
        }
        return c
    
    }
    

    you have defined the same struct name to Clusters struct that's why the error

    cmd/directories.go:43:20: Cluster is not a type

    Checkout working code on Go playground

    In Golang Composite literal is defined as:

    Composite literals construct values for structs, arrays, slices, and maps and create a new value each time they are evaluated. They consist of the type of the literal followed by a brace-bound list of elements. Each element may optionally be preceded by a corresponding key.

    Also Have a look on struct literals section defined in above link for Compositeliterals to get more description.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题