For code append(slice1, 1)
, Go compile will give error "append(...) evaluated but not used". And we have to use like slice1 = append(slice1,1)
because append
doesn't modify slice1 and it will return a new slice.
I think it is a good hint since this will prevent lots of bug since we often didn't know function like append
will change original array or not. In JavaScript array1.push('item')
will change array1 in place and return new length of the array.
I want to utilize this kind of code checking:
func appendStr(str string, tail string) string {
b := str + tail
return b
}
a := "a"
appendStr(a, "b")
But Go compiler didn't give error. So compiler do some special checking on append
method? Since Go pass parameter by value, Compiler should know appendStr
has no change to modify pass-in parameter.