package main
import "fmt"
func main() {
testAppend()
testCopy()
testDelete()
}
func testAppend() {
var sl = make([]int,2)
sl[0] = 1
sl[1] = 2
fmt.Println(len(sl))
fmt.Println(cap(sl))
sl = append(sl, 3)
//sl = append(sl, 4)
fmt.Println(sl)
fmt.Println(len(sl))
fmt.Println(cap(sl))
}
func testCopy() {
var sl1 = make([]string,3)
var sl2 = make([]string,2)
sl1[0] = "a"
sl1[1] = "b"
sl1[2] = "e"
sl2[0] = "c"
sl2[1] = "d"
copy(sl1,sl2)
fmt.Println(sl1)
}
func testDelete() {
var sl = make(map[int]int ,2)
sl[1] = 1
sl[2] = 2
sl[3] = 3
delete(sl, 2)
fmt.Println(sl)
}
结果如下
GOROOT=/usr/local/go #gosetup
GOPATH=/www/gopath #gosetup
/usr/local/go/bin/go build -i -o /private/var/folders/fc/4txmmczj6q92p6058h3w7t_80000gn/T/___go_build_main_go__2_ /www/go/learn/main.go #gosetup
/private/var/folders/fc/4txmmczj6q92p6058h3w7t_80000gn/T/___go_build_main_go__2_ #gosetup
2
2
[1 2 3]
3
4
[c d e]
map[1:1 3:3]
Process finished with exit code 0