golang append() evaluated but not used

后端 未结 5 1346
失恋的感觉
失恋的感觉 2021-02-01 12:12
func main(){
     var array [10]int
     sliceA := array[0:5]
     append(sliceA, 4)
     fmt.Println(sliceA)
}

Error : append(sliceA, 4

5条回答
  •  臣服心动
    2021-02-01 12:39

    you may try this:

    sliceA = append(sliceA, 4)
    

    built-in function append([]type, ...type) returns an array/slice of type, which should be assigned to the value you wanted, while the input array/slice is just a source. Simply, outputSlice = append(sourceSlice, appendedValue)

提交回复
热议问题