This question already has an answer here:
main declares a slice with name allOutputs
(i believe its a slice of strings, not an array of strings) with zero length and 100 capacity. Then it appends a string with value "abcd" and calls myTest function which updates the array[0] with "1234" and then does an append with value "5678".
When i printed allOutputs
after myTest
call, i correctly see the element at the first index has an updated value of "1234". This tells me that myTest got slice as reference. But the later append
of "5678"
is not seen by the caller (main here) at all why its so? Remember the original slice is backed by an array with a capacity of 100. Why can't i see 5678 in main when the slice is passed by a reference?
In other words, how exactly the append works?
import "fmt"
func myTest(array []string) {
array[0] = "1234"
array = append(array, "5678")
}
func main() {
allOutputs := make([]string, 0, 100)
allOutputs = append(allOutput, "abcd")
fmt.Println(allOutputs) // Println1
myTest(allOutputs)
fmt.Println(allOutputs) // Println2
}
ACTUAL OUTPUT: [1234]
I EXPECTED: [1234, 5678]
</div>