Skip to content

Instantly share code, notes, and snippets.

@shelomentsevd
Created November 28, 2017 15:47
Show Gist options
  • Save shelomentsevd/d77eaf6ce9e29cb67962beb6dc6d4213 to your computer and use it in GitHub Desktop.
Save shelomentsevd/d77eaf6ce9e29cb67962beb6dc6d4213 to your computer and use it in GitHub Desktop.
Go Fuckup #1 appending to slice
package main
import "fmt"
type Box struct {
ID string
}
type BoxContainter struct {
box Box
v int
}
func main() {
slice := make([]BoxContainter, 0)
slice = append(slice,
BoxContainter{
box: Box{
"1",
},
v: 1,
},
BoxContainter{
box: Box{
"2",
},
v: 2,
},
BoxContainter{
box: Box{
"3",
},
v: 3,
},
)
slice2 := make([]*Box, 0)
for _, v := range slice {
slice2 = append(slice2, &v.box)
}
for _, v := range slice2 {
fmt.Println(v.ID)
}
slice2 = make([]*Box, 0)
for i, _ := range slice {
slice2 = append(slice2, &slice[i].box)
}
for _, v := range slice2 {
fmt.Println(v.ID)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment