(Go)04.go工作区目录规范及简单例子
一.规范目录结构
D:\project\src\go_dev\day1\example1
二.设置GOPAH环境变量
三.hello world
1.hello world
1 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