How to implement resizable arrays in Go

前端 未结 6 1818
遥遥无期
遥遥无期 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

    Hi we can simply do this in two ways

    type mytype struct {
      a, b int
    }
    

    Just do like this

    1. Without append

    __

    a := []mytype{mytype{1, 2}, mytype{3, 4}, mytype{4, 5}}
    
    1. With append

    __

    a:=  append([]mytype{}, mytype{1, 2}, mytype{3, 4}, mytype{4, 5})
    

    Add as much as you want. First one is an easy way to do this. Hope this will help you.

提交回复
热议问题