2 回答
TA贡献1993条经验 获得超5个赞
你有两种方法:
使用
cleaned := make([]interface{},0)
and 调用cleaned = append(cleaned, your_data)
声明一个空数组:
var cleaned []interface{}{}
TA贡献1805条经验 获得超9个赞
cleaned := []interface{}{}
我怎样才能用 make() 做同样的事情?
例如,
package main
import (
"fmt"
)
func main() {
{
cleaned := []interface{}{}
fmt.Println(len(cleaned), cap(cleaned), cleaned)
}
// OR
{
cleaned := make([]interface{}, 0)
fmt.Println(len(cleaned), cap(cleaned), cleaned)
}
// OR
{
cleaned := make([]interface{}, 0, 0)
fmt.Println(len(cleaned), cap(cleaned), cleaned)
}
}
游乐场: https: //play.golang.org/p/qndsxIKWjEp
输出:
0 0 []
0 0 []
0 0 []
声明空切片时,更喜欢
var t []string超过
t := []string{}前者声明一个 nil 切片值,而后者非 nil 但长度为零。它们在功能上是等效的——它们的 len 和 cap 都为零——但 nil 切片是首选样式。
. . .
例如,
package main
import (
"fmt"
)
func main() {
{
cleaned := []interface{}{}
fmt.Println(len(cleaned), cap(cleaned), cleaned, cleaned == nil)
}
// OR
{
cleaned := make([]interface{}, 0)
fmt.Println(len(cleaned), cap(cleaned), cleaned, cleaned == nil)
}
// OR
{
cleaned := make([]interface{}, 0, 0)
fmt.Println(len(cleaned), cap(cleaned), cleaned, cleaned == nil)
}
// OR
{
var cleaned []interface{}
fmt.Println(len(cleaned), cap(cleaned), cleaned, cleaned == nil)
}
}
游乐场:https://play.golang.org/p/9cZOOS2F9qI
输出:
0 0 [] false
0 0 [] false
0 0 [] false
0 0 [] true
- 2 回答
- 0 关注
- 134 浏览
添加回答
举报