He escrito el siguiente código. Pero no puedo tenerlo compilado. Aquí está mi código:Problemas sobre `slice` y` append` en Go
package main
import "fmt"
func main() {
tmp := make([]int, 10)
for i := 0; i < 10; i++ {
tmp[i] = i
}
res := mapx(foo, tmp)
fmt.Printf("%v\n", res)
}
func foo(a int) int {
return a + 10
}
func mapx(functionx func(int) int, list []int) (res []int) {
res = make([]int, 10)
for _, i := range(list) {
append(res, functionx(i))
}
return
}
Mientras tanto, el mensaje de error es también muy confuso: prog.go:21: append(res, functionx(i)) not used
Pero si reemplazo append(res, functionx(i))
(línea 21) con res = append(res, functionx(i))
, que funciona bastante bien. Alguien puede ayudarme?
¡Gracias!