在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一.规范目录结构D:\project\src\go_dev\day1\example1 二.设置GOPAH环境变量
三.hello world1.hello world1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func main () { 8 fmt.Println("Hello World!") 9 } go build go_dev\day1\example1
生成example1.exe 执行example1.exe
2.goroute循环goroute.go 1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func test_goroute(a int) { 8 fmt.Println(a) 9 } main.go 1 package main 2 3 import ( 4 "time" 5 ) 6 7 8 func main() { 9 10 for i :=0; i <100; i++ { 11 go test_goroute(i) 12 } 13 time.Sleep(2*time.Second) 14 }
3.goroute_example代码目录结构 1 package goroute 2 3 4 func Add(a int, b int, c chan int) { 5 sum := a + b 6 c <- sum 7 8 } 1 package main 2 3 import ( 4 "go_dev/day1/goroute_example/goroute" 5 "fmt" 6 ) 7 8 9 func main() { 10 //var pipe chan int 11 //pipe = make(chan int, 1) 12 13 pipe := make(chan int, 1) //等同于上二行 14 go goroute.Add(100, 300, pipe) 15 16 17 sum := <- pipe 18 fmt.Println("sum =", sum) 19 } 构建代码 D:\project>go build -o bin/goroute_example.exe go_dev/day1/goroute_example/main D:\project\bin>goroute_example.exe sum = 400
|
请发表评论