Golang下的slice拼接

2018-11-23  本文已影响20人  9c46ece5b7bd

我们使用Python编程都知道,想要拼接两个list为一个list可以有很多方式,比如:

# 方式1 :
name = ["golang","python","shell"]
user = ["xxb","biaoge"]
alldata = name+user
print(alldata)
# 方式2:
name = ["golang","python","shell"]
user = ["xxb","biaoge"]
name.extend(user)
print(name)

在Golang中如何快速的拼接slice呢,其实有个很巧妙的方法:

$ cat addslice.go
package main
import (
    "fmt"
)
func main() {
    name := []string{"golang","python","shell"}
    user := []string{"xxb","bgbiao"}
    hah := append(name,user...)
    fmt.Println(hah)
    fmt.Println(hah[0])
}

$ go run /tmp/abc.go
[golang python shell xxb bgbiao]
golang
上一篇下一篇

猜你喜欢

热点阅读