英文:
Defining a function that returns a slice of variable size in golang
问题
我想要构建一个可以返回任意大小切片的函数。我知道可以这样做:
func BuildSlice() [100]int { return [100]int{} }
但是我希望能够从同一个函数返回不同大小的切片。类似这样:
func BuildSlice(size int) []int { return make([]int, size) }
我尝试了上述方法以及:
func BuildSlice(size int) []int { return [size]int{} }
请指点我正确的方向。
英文:
I would like to build a function that returns a slice of any size. I know I can do
func BuildSlice() [100]int { return [100]int{} }
but I would like to be able to return slices of different sizes from the same function. Something like:
func BuildSlice(int size) [...]int { return [size]int{} }
I've tried the above as well as
func BuildSlice(size int) []int { return [size]int{} }
Please point me in the right direction.
答案1
得分: 54
首先,切片已经是“可变大小”的:[100]int
和[...]int
是数组类型的定义。
[]int
是切片的正确语法,你可以这样实现该函数:
func BuildSlice(size int) []int {
return make([]int, size)
}
这将返回一个具有所需大小的零值切片,类似于你的数组版本。
英文:
First of all, slices are already of "variable size": [100]int
and [...]int
are array type definitions.
[]int
is the correct syntax for a slice, and you could implement the function as:
func BuildSlice(size int) []int {
return make([]int, size)
}
This will return a slice of zero values with the desired size, similar to what your array version does.
答案2
得分: 5
《Go编程语言规范》
创建切片、映射和通道
内置函数make接受一个类型T作为参数,该类型必须是切片、映射或通道类型,可选地跟随一个特定类型的表达式列表。它返回一个类型为T的值(而不是*T)。内存的初始化方式如初始值节所述。
调用 类型T 结果
make(T, n) 切片 长度为n、容量为n的类型为T的切片
make(T, n, m) 切片 长度为n、容量为m的类型为T的切片
大小参数n和m必须是整数类型或无类型的。常量大小参数必须是非负的,并且可以由int类型的值表示。如果提供了n和m,并且它们都是常量,则n必须不大于m。如果n在运行时为负数或大于m,则会发生运行时恐慌。
例如:
s := make([]int, 10, 100) // 切片的长度为10,容量为100
s := make([]int, 1e3) // 切片的长度和容量都为1000
s := make([]int, 1<<63) // 非法:切片的长度不能由int类型的值表示
s := make([]int, 10, 0) // 非法:切片的长度大于容量
例如:
package main
import "fmt"
func main() {
s := make([]int, 7, 42)
fmt.Println(len(s), cap(s))
t := make([]int, 100)
fmt.Println(len(t), cap(t))
}
输出:
7 42
100 100
英文:
> The Go Programming Language Specification
>
> Making slices, maps and channels
>
> The built-in function make takes a type T, which must be a slice, map
> or channel type, optionally followed by a type-specific list of
> expressions. It returns a value of type T (not *T). The memory is
> initialized as described in the section on initial values.
>
> Call Type T Result
>
> make(T, n) slice slice of type T with length n and capacity n
> make(T, n, m) slice slice of type T with length n and capacity m
>
> The size arguments n and m must be of integer type or untyped. A
> constant size argument must be non-negative and representable by a
> value of type int. If both n and m are provided and are constant, then
> n must be no larger than m. If n is negative or larger than m at run
> time, a run-time panic occurs.
>
> s := make([]int, 10, 100) // slice with len(s) == 10, cap(s) == 100
> s := make([]int, 1e3) // slice with len(s) == cap(s) == 1000
> s := make([]int, 1<<63) // illegal: len(s) is not representable by a value of type int
> s := make([]int, 10, 0) // illegal: len(s) > cap(s)
For example,
package main
import "fmt"
func main() {
s := make([]int, 7, 42)
fmt.Println(len(s), cap(s))
t := make([]int, 100)
fmt.Println(len(t), cap(t))
}
Output:
7 42
100 100
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论