Skip to content

Instantly share code, notes, and snippets.

@mumubin
Last active October 16, 2019 13:48
Show Gist options
  • Save mumubin/6094f3601f15c1810dc945e836c47538 to your computer and use it in GitHub Desktop.
Save mumubin/6094f3601f15c1810dc945e836c47538 to your computer and use it in GitHub Desktop.
go learn

GO Test

go test -coverprofile=c.out             
go tool cover -html=c.out 

go test -bench . -cpuprofile=cpu.profile
go tool pprof cpu.profile

Example

func ExampleQueue_Pop() {
	q := Queue{1}
	q.Push(2)
	q.Push(3)
	fmt.Println(q.Pop())
	fmt.Println(q.Pop())
	fmt.Println(q.IsEmpty())
	fmt.Println(q.Pop())
	fmt.Println(q.IsEmpty())

	//Output:
	//1
	//2
	//false
	//31
	//true
}

goroutine可能切换的点

  • I/O,select
  • channel
  • 等待锁
  • 函数调用(有时)
  • runtime.Gosched() 只是参考,不保证切换,其它地方也不保证不切换
go run -race goroutine.go

传统同步机制

不要使用共享内存来同步数据,使用chan来同步

  • WaitGroup
  • Mutex
  • Cond

http 性能调优

  • pprof
http://localhost:8888/debug/pprof
import(
_ "net/http/pprof"
)

or
go tool pprof http://localhost:8888/debug/pprof/profile
go tool pprof http://localhost:8888/debug/pprof/heap

看文档

godoc -http :8888

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment