Sobes.tech
Senior

package main import ( "fmt" "sync" ) func change(sl []int){ sl[0] = 10 } func main() { sl := make([]int, 10) sl[0] = 5 //5 change(sl) //1 fmt.Println(sl) }

sobes.tech AI

Answer from AI

In this example, the change function takes a slice sl and modifies its first element to 10. In Go, slices are passed by value, but they contain a pointer to the array, along with length and capacity. Therefore, changes to slice elements inside the function affect the original array.

In main, a slice of length 10 is created, the first element is set to 5, then change(sl) is called, which changes the first element to 10. As a result, when printing the slice, it will be:

[10 0 0 0 0 0 0 0 0 0]

This demonstrates that changes inside the function affect the original slice, since slices in Go are references to arrays.

package main import ( "fmt" "sync" ) func change(sl… - sobes.tech