2 回答

TA貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊
你有兩種方法:
使用
cleaned := make([]interface{},0)
and 調(diào)用cleaned = append(cleaned, your_data)
聲明一個(gè)空數(shù)組:
var cleaned []interface{}{}

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊
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)
? ? }
}
游樂(lè)場(chǎng): https: //play.golang.org/p/qndsxIKWjEp
輸出:
0 0 []
0 0 []
0 0 []
聲明空切片時(shí),更喜歡
var?t?[]string超過(guò)
t?:=?[]string{}前者聲明一個(gè) nil 切片值,而后者非 nil 但長(zhǎng)度為零。它們?cè)诠δ苌鲜堑刃У摹鼈兊?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)
? ? }
}
游樂(lè)場(chǎng):https://play.golang.org/p/9cZOOS2F9qI
輸出:
0 0 [] false
0 0 [] false
0 0 [] false
0 0 [] true
- 2 回答
- 0 關(guān)注
- 190 瀏覽
添加回答
舉報(bào)