Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Arrays in Go (aaronoellis.com)
32 points by aodin on July 28, 2014 | hide | past | favorite | 2 comments


I've been using a pattern like this for my more complex initialisations:

s, i := make([]T, 0, 6), 0

s = append(s, 1, 2, 3)

//[1 2 3]

fmt.Println(s[i:])

i += 3

s = append(s, 4, 5, 6)

//[4 5 6]

fmt.Println(s[i:])

i += 3 // [etc...]

//[1 2 3 4 5 6]

fmt.Println(s[:])

You can easily make references to each chunk of the slice you append without much difficulty or any re-sizing (the first s[:] will remain a slice of the first 3 items).


The []int{1,2,3} method for initializing slices was missed: http://play.golang.org/p/FEXc1sxVLK




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: