【发布时间】:2021-05-03 07:24:26
【问题描述】:
package main
import (
"fmt"
"math/rand"
)
func randoms() *[]int {
var nums []int = make([]int, 5, 5) //Created slice with fixed Len, cap
fmt.Println(len(nums))
for i := range [5]int{} {//Added random numbers.
nums[i] = rand.Intn(10)
}
return &nums//Returning pointer to the slice
}
func main() {
fmt.Println("Hello, playground")
var nums []int = make([]int, 0, 25)
for _ = range [5]int{} {//Calling the functions 5 times
res := randoms()
fmt.Println(res)
//nums = append(nums, res)
for _, i := range *res {//Iterating and appending them
nums = append(nums, i)
}
}
fmt.Println(nums)
}
我试图模仿我的问题。我有动态数量的函数调用,即randoms 和动态数量的结果。我需要附加所有结果,即numbers in this case。
我可以用iteration 做到这一点,而且没有问题。我正在寻找一种方法来做类似nums = append(nums, res) 的事情。有没有办法做到这一点/任何内置方法/我误解了指针吗?
【问题讨论】:
标签: go