for _, val := range str_slice {
anyThing = append(anyThing, val)
}
fmt.Println(anyThing)
len(str_slice) = 4. We don’t have this issue with len attire set to 0 in make()
When appending, can’t it detect automatically that it s empty and place in first position ? Any proposal to change this behaviour in latest go version ?
after str_slice := make([]string,1)
the slice isn’t empty: it has one entry with the “zero” value of “” for a string.
if you want an empty slice, try str_slice := make([]string, 0, 1)
Jonathan.
Yes. Makes sense thats why i mentioned if len=0 in make() its different.
Go initialise with default value of that type. ( different from C)
What I was trying to point out is, when append is made why can’t it over write the first “” entry and make X as its first entry.
Its because its complied language. initialisation should make a default value entry for this slice with the type of data it holds.
Coming from python background i thought of this - which happens dynamically at runtime.
listString = []
listString.append("abc)
listString.append("xyz)
print(listString)
–> [“abc”, “xyz”]