写在前面
由于论文是区块链方向, Go 就是个需要跨过的门槛, 不得不说 Go 是个不错的语言, 写起来也很舒服, 就以 hello world 开始吧.
- Hello World
- package main
- /**
- * 通道通信, 不得不说 go 语言的神奇之处, 这或许就是 go 语言的由来吧!!
- */
- import "fmt"
- func sum(s []int, c chan int) {
- sum := 0
- for _, v := range s {
- sum += v
- }
- c <- sum // 把 sum 发送到通道 c
- }
- func main() {
- s := []int{1, 2, 3, 4, 5}
- c := make(chan int)
- go sum(s[:len(s)/2], c)
- go sum(s[len(s)/2:], c)
- x, y := <-c, <-c // 从通道 c 中接收
- fmt.Println(x, y, x+y)
- }
helloworld 源码
Git clone Git@GitHub.com:hulichao/gopath.Git
来源: http://www.bubuko.com/infodetail-2995593.html