gpt4 book ai didi

go - Aerospike golang ListInsertOp 多个值

转载 作者:IT王子 更新时间:2023-10-29 01:38:30 25 4
gpt4 key购买 nike

golang aerospike客户端中的ListInsertOp是否支持插入元素数组?

如果我使用 https://godoc.org/github.com/aerospike/aerospike-client-go#ListAppendOp ,并传递一个数组 ([]string),它只是将整个数组作为一个值附加到列表中。我是用错了还是有其他方法可以做到这一点?

最佳答案

ListAppendOp 是一个可变参数函数,它接受任意数量的 interface{} 类型的参数。如果您调用它来传递您的数组,它将收到一片 interface{} ([]interface{}),其中有一个元素是您的数组。

您需要将数组转换为 interface{} 的一部分,并在将其传递给函数时使用 ... 展开它:

a := []string{"a", "b", "c"}

s := make([]interface{}, len(a))
for i, v := range a {
s[i] = v
}

ListAppendOp("bin1", s...)

将数组传递给可变函数的示例: https://play.golang.org/p/541aJ6dY6D

从规范: Passing arguments to ... parameters

关于go - Aerospike golang ListInsertOp 多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46662690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com