github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/tests/files/a47.gno (about)

     1  package main
     2  
     3  type S struct {
     4  	i int
     5  }
     6  
     7  func main() {
     8  	sArr := make([]S, 0, 4)
     9  	sArr = append(sArr, S{1}, S{2}, S{3})
    10  	println(sArr[0].i, sArr[1].i, sArr[2].i)
    11  
    12  	newArr := append(sArr[:0], sArr[1:]...)
    13  
    14  	// The append modified the underlying array because it was within capacity.
    15  	println(len(sArr) == 3)
    16  	println(sArr[0].i, sArr[1].i, sArr[2].i)
    17  
    18  	// It generated a new slice that references the same array.
    19  	println(len(newArr) == 2)
    20  	println(newArr[0].i, newArr[1].i)
    21  
    22  	// The struct should have been copied, not referenced.
    23  	println(&sArr[2] == &newArr[1])
    24  	// share same underlying array, and same index
    25  	println(&sArr[1] == &newArr[1])
    26  }
    27  
    28  // Output:
    29  // 1 2 3
    30  // true
    31  // 2 3 3
    32  // true
    33  // 2 3
    34  // false
    35  // true