Created
November 28, 2017 15:47
-
-
Save shelomentsevd/d77eaf6ce9e29cb67962beb6dc6d4213 to your computer and use it in GitHub Desktop.
Go Fuckup #1 appending to slice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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