patterngoCritical
golang append() evaluated but not used
Viewed 0 times
evaluatedusedgolangappendnotbut
Problem
func main(){
var array [10]int
sliceA := array[0:5]
append(sliceA, 4)
fmt.Println(sliceA)
}Error : append(sliceA, 4) evaluated but not used
I don't Know why? The slice append operation is not run...
Solution
Refer: Appending to and copying slices
In Go, arguments are passed by value.
Typical
You need to write:
Output:
In Go, arguments are passed by value.
Typical
append usage is:a = append(a, x)You need to write:
func main(){
var array [10]int
sliceA := array[0:5]
// append(sliceA, 4) // discard
sliceA = append(sliceA, 4) // keeping return value is mandatory
fmt.Println(sliceA)
}Output:
[0 0 0 0 0 4]Code Snippets
a = append(a, x)func main(){
var array [10]int
sliceA := array[0:5]
// append(sliceA, 4) // discard
sliceA = append(sliceA, 4) // keeping return value is mandatory
fmt.Println(sliceA)
}[0 0 0 0 0 4]Context
Stack Overflow Q#45004854, score: 142
Revisions (0)
No revisions yet.