go make方法

package main

import "fmt"

func main()  {

    // make函数
    makeSlice() // 创建切片
    makeMap() // 创建集合
    makeChan() // 创建channel
}

func makeSlice(){
    sl := make([]string,3)
    sl[0] = "a";
    sl[1] = "b";
    sl[2] = "c";
    fmt.Println(sl)

}

func makeMap(){
    mp  := make(map[int] string)

    mp[0] = "hello"
    mp[1] = "world"
    mp[33] = "!"
    fmt.Println(mp)
}

func makeChan()  {
    mchan := make(chan string)

    go func() {
        mchan <- "hello world"
    }()

    message := <- mchan

    fmt.Println(message)
}
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
[a b c]
map[0:hello 1:world 33:!]
hello world

Process finished with exit code 0

  

上一篇:CF915E 动态开线段树


下一篇:知识点 - 划分树