How to implement resizable arrays in Go

前端 未结 6 1845
遥遥无期
遥遥无期 2020-12-08 08:57

I come from a C++ background and I\'m used to using the std::vector class for things like this. Lets assume I want a dynamic array of these:

typ         


        
6条回答
  •  鱼传尺愫
    2020-12-08 09:34

    Use the append() builtin

    Example:

    type mytype struct {
      a, b int
    }
    
    func main() {
      a := []mytype{mytype{1, 2}, mytype{3, 4}}
      a = append(a, mytype{5, 6})
    }
    

    Refer to the spec for more info on append.

提交回复
热议问题