I think this question where asked several times but I still confused:
I have following code:
type obj struct {
s *string
}
var cmdsP = []*string {
stringPointer("create"),
stringPointer("delete"),
stringPointer("update"),
}
var cmds = []string {
"create",
"delete",
"update",
}
// []*string
func loop1() {
slice := make([]obj, 0, 0)
for _, cmd := range cmdsP {
slice = append(slice, obj{cmd})
}
for _, o := range slice {
fmt.Println(*o.s)
}
}
// []string
func loop2() {
slice := make([]obj, 0, 0)
for _, cmd := range cmds {
slice = append(slice, obj{&cmd})
}
for _, o := range slice {
fmt.Println(*o.s)
}
}
func stringPointer(v string) *string {
return &v
}
https://play.golang.org/p/65Le_8Pi3Mi
The only difference is in slice semantic []*string
and []string
how does it change behavior of cmd
variable? Could you please draw or explain in details what happens in memory during iteration through two loops?