Go语言入门-工程实践 | 青训营笔记

97 阅读1分钟

这是我参与「第五届青训营 」伴学笔记创作活动的第 2 天

Go语言入门-工程实践

1、语言进阶

并发编程

并发:多线程程序在一个核的cpu上运行

并行:多线程程序在多个核的cpu上运行

协程:用户态,轻量级线程,栈KB级别

线程:内核态,线程跑多个协程,栈MB级别

goroutine

CSP(Communicating Sequential Processes):提倡通过通信共享内存

Channel:管道,分无缓冲和有缓冲管道

  1. send语句

    send语句用来往Channel中发送数据, 如ch <- 3

  2. receive 操作符

    <-ch用来从channel ch中接收数据,这个表达式会一直被block,直到有数据可以接收。

并发安全Lock:Lock()、Unlock()

WaitGroup:Add(),Done(),Wait()

2、依赖管理

依赖管理演进:GOPATH->GO Vendor->Go Module

GOPATH:

bin:项目编译的二进制文件

pkg:项目编译的中间产物,加速编译

src:项目源码

弊端:无法实现package的多版本控制

Go Vendor

弊端:无法控制依赖的版本

更新项目又可能出现依赖冲突,导致编译出错

Go Module

三要素:

1、配置文件,描述依赖 go.mod

2、中兴仓库管理依赖库 Proxy

3、本地工具 go get/mod

工具:

   |--init       初始化,创建go.mod文件 
   

go mod |--download 下载模块到本地缓存

   |--tidy       增加需要的依赖,删除不需要的依赖
   

3、测试

单元测试

规则:

1、测试文件以_test.go结尾

2func TestXxx(*testing.T)

3、初始化逻辑放到TestMain中

go test xxx_test.go xxx.go --cover

Mock测试

monkey:github.com/bouk/monkey

Patch()

Unpatch()

基准测试

go test -bench=. xxx_test.go xxx.go

package benchmark |                                            |
| ----------------- | ------------------------------------------ |
|                   |                                            |
|                   | import (                                   |
|                   | "github.com/bytedance/gopkg/lang/fastrand" |
|                   | "math/rand"                                |
|                   | )                                          |
|                   |                                            |
|                   | var ServerIndex [10]int                    |
|                   |                                            |
|                   | func InitServerIndex() {                   |
|                   | for i := 0; i < 10; i++ {                  |
|                   | ServerIndex[i] = i+100                     |
|                   | }                                          |
|                   | }                                          |
|                   |                                            |
|                   | func Select() int {                        |
|                   | return ServerIndex[rand.Intn(10)]          |
|                   | }                                          |
|                   |                                            |
|                   | func FastSelect() int {                    |
|                   | return ServerIndex[fastrand.Intn(10)]      |
|                   | }
package benchmark |                                                  |
| ----------------- | ------------------------------------------------ |
|                   |                                                  |
|                   | import (                                         |
|                   | "testing"                                        |
|                   | )                                                |
|                   |                                                  |
|                   | func BenchmarkSelect(b *testing.B) {             |
|                   | InitServerIndex()                                |
|                   | b.ResetTimer()                                   |
|                   | for i := 0; i < b.N; i++ {                       |
|                   | Select()                                         |
|                   | }                                                |
|                   | }                                                |
|                   | func BenchmarkSelectParallel(b *testing.B) {     |
|                   | InitServerIndex()                                |
|                   | b.ResetTimer()                                   |
|                   | b.RunParallel(func(pb *testing.PB) {             |
|                   | for pb.Next() {                                  |
|                   | Select()                                         |
|                   | }                                                |
|                   | })                                               |
|                   | }                                                |
|                   | func BenchmarkFastSelectParallel(b *testing.B) { |
|                   | InitServerIndex()                                |
|                   | b.ResetTimer()                                   |
|                   | b.RunParallel(func(pb *testing.PB) {             |
|                   | for pb.Next() {                                  |
|                   | FastSelect()                                     |
|                   | }                                                |
|                   | })                                               |
|                   | }

4、项目实战