天然并发
- 从语言层面支持并发,
- goroute,轻量级线程
- 基于CSP模拟实现
channel
- 管道,类似于linux的pipe
- 多个goroute之间通过channel进行通信
- 支持任何类型
管道
func test_pipr() {
//管道放入数据
pipe := make(chan int,3)
pipe <- 1
pipe <- 2
pipe <- 3
//先进先出
//pipe <- 4
//fatal error: all goroutines are asleep - deadlock!
fmt.Println(len(pipe))
var t int
t =<- pipe
fmt.Println(t)
fmt.Println(len(pipe))
}
文件整理 gofmt -w file.go
go的多线程,可以使用管道来引出数据
多值返回
var c int = a + b
var d int = a + a + b
return c,d
}
func main() {
a,b := many_return(100,200)
fmt.Println("a=",a,"b=",b)
}
常量
- const 只能修饰boolean,number,(int相关类型、浮点类型、complex)和string
const identifier [type] = value
其中type可以省略eg:
const b string = "hello world"
const b = "hello world"
const Pi = 3.1415926
const a = 9/3
值类型和引用类型
- 值类型:基本数据类型int、float、bool、string以及数组和struct
- 引用类型:指针、slice、map、chan等