Go语言基础
Posted 想考北航的小刺猬
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Go语言基础相关的知识,希望对你有一定的参考价值。
Concurrency
Goroutines
goroutine是一个轻量级的线程通过Go runtime(运行时)管理
package main
import (
"fmt"
"time"
)
func say(s string)
for i := 0; i < 5; i++
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
func main()
go say("world")
say("hello")
Channels
Channel是一种有类型的管道,通过它,可以用运算符<-发送和接收数据。
ch <- v // Send v to channel ch.
v := <-ch // Receive from ch, and
// assign value to v.
channel必须在用之前先创建:
ch := make(chan int)
默认情况下,发送和接收阻塞,直到另一方准备好。这允许goroutines同步,而不需要显式的锁或条件变量。
package main
import "fmt"
func sum(s []int, c chan int)
sum := 0
for _, v := range s
sum += v
c <- sum // send sum to c
func main()
s := []int7, 2, 8, -9, 4, 0
c := make(chan int)
go sum(s[len(s)/2:], c)
go sum(s[:len(s)/2], c)
x, y := <-c, <-c // receive from c
fmt.Println(x, y, x+y)
Buffer Channels
可以给通道一个长度。
ch := make(chan int, 100)
Range and Close
发送方可以关闭通道,以表示不再发送任何值。接收器可以通过给receive表达式赋值第二个参数来测试通道是否已经关闭
v, ok := <-ch
根据OK的值来判断是否关闭,之应该允许sender关闭通道而不允许receiver关闭。channel不像文件,通常不需要关闭,只有当接收者再也不接收数据了,就可以关闭了。
package main
import (
"fmt"
)
func fibonacci(n int, c chan int)
x, y := 0, 1
for i := 0; i < n; i++
c <- x
x, y = y, x+y
close(c)
func main()
c := make(chan int, 10)
go fibonacci(cap(c), c)
for i := range c
fmt.Println(i)
Select
select语句允许goroutine等待多个通信操作。select会阻塞直到其中一个用例能运行,然后执行这个用例,多个用例准备好则随机选一个。
import "fmt"
func fibonacci(c, quit chan int)
x, y := 0, 1
for
select
case c <- x:
x, y = y, x+y
case <-quit:
fmt.Println("quit")
return
func main()
c := make(chan int)
quit := make(chan int)
go func()
for i := 0; i < 10; i++
fmt.Println(<-c)
quit <- 0
()
fibonacci(c, quit)
- default case:如果没有用例准备好,就运行default case
sync.Mutex
信号量,互斥锁
- Lock
- Unlock
package main
import (
"fmt"
"sync"
"time"
)
// SafeCounter is safe to use concurrently.
type SafeCounter struct
mu sync.Mutex
v map[string]int
// Inc increments the counter for the given key.
func (c *SafeCounter) Inc(key string)
c.mu.Lock()
// Lock so only one goroutine at a time can access the map c.v.
c.v[key]++
c.mu.Unlock()
// Value returns the current value of the counter for the given key.
func (c *SafeCounter) Value(key string) int
c.mu.Lock()
// Lock so only one goroutine at a time can access the map c.v.
defer c.mu.Unlock()
return c.v[key]
func main()
c := SafeCounterv: make(map[string]int)
for i := 0; i < 1000; i++
go c.Inc("somekey")
time.Sleep(time.Second)
fmt.Println(c.Value("somekey"))
以上是关于Go语言基础的主要内容,如果未能解决你的问题,请参考以下文章