HiveBrain v1.2.0
Get Started
← Back to all entries
snippetgoCritical

How to implement resizable arrays in Go

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
implementarraysresizablehow

Problem

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:

type a struct {
    b int
    c string
}


What is the standard way of doing this?

A snippet would be very useful

Solution

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.

Code Snippets

type mytype struct {
  a, b int
}

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

Context

Stack Overflow Q#3387273, score: 108

Revisions (0)

No revisions yet.