go语言入门经典_GO语言入门

一、GO基础

1. GOPATH

2. GO 命令源码文件

1)命令源码文件定义:

命令源码文件是程序的运行入口,如果一个源码文件声明属于main包,并且包含一个无参数声明的main函数,那么它就是命令源码文件

2)命令源码文件接收参数的包:

GO语言标准库中有一个代码包flag专门用于接收和解析程序参数

A. flag.StringVar()
 flag.StringVar(&name, "name", "everyone", "The greeting object.")

需要4个参数:

  • 1)用于存储该命令参数的值地址,一般取先前定的变量的地址。如先前生命var name string; 则取&name
  • 2)指定该命令参数名称
  • 3)该命令参数的默认值
  • 4)该命令参数的简短说明
B. flag.String(): 直接返回一个已给分配好的用于存储命令参数值的地址
 flag.String("name", "everyone", "The greeting object.")
3)命令源码文件传入参数及查看参数说明C. flag.Parse(): 用于真正解析命令参数,并把它们的值赋给相应的变量
a. demo2.go文件
package mainimport ("flag""fmt")func init() {flag.StringVar(&name, "name", "everyone", "The greeting object.")}var name stringfunc main() {flag.Parse()fmt.Printf("Hello, %s!", name)}
b. 传参数name,执行
$go run demo2.go -name="wujun"$Hello, wujun!
C. 执行help查看命令参数说明
$go run demo2.go --help$Usage of /var/folders/tc/25kk9vz522v3f6j_tffxzrwr0000gn/T/go-build732294726/b001/exe/demo2:  -name string        The greeting object. (default "everyone")exit status 2

二、 GO常用工具

1. 常用字符串函数处理包

  • strings
  • strconv

2. 常用Go依赖管理工具

  • godep
  • glide
  • dep

3. GO相关库

  • GO测试断言库:https://github.com/stretchr/testify
  • GO BDD相关库:https://github.com/smartystreets/goconvey
  • GO Json库:https://github.com/mailru/easyjson
  • GO Router库:https://github.com/julienschmidt/httprouter
  • GO相关限流库:https://github.com/didip/tollbooth

4. 测试相关

  • 单元测试 : go test
  • Bechmark测试 , 包括性能消耗: go test -bench=. -benchmem

三、GO GC

1. 避免内存分配和复制

(1) 复杂对象尽量传递引用
  • 数组的传递
  • 结构体的传递
(2) 初始化至合适的大小
  • 自动扩容是有代价的
(3) 复制内存

2. 打开GC日志

(1) 加上GODEBUG环境变量
GODEBUG=gctrace=1 go test -bench=.GODEBUG=gctrace=1 go run main.go
(2) 日志详情信息参考:https://godoc.org/runtime

3. go tool trace

(1) 普通程序输出trace信息
1d59dc0f295a4bbbb56aa7b5b365f157.png
(2) 测试程序输出trace信息
go test -trace trace.out
(3) 可视化trace信息
go tool trace trace.out

四、GO 性能调优

1)安装graphviz

brew install graphviz

3)安装go-torch2) 将GOPATH/bin 加入 $PATH

  • go get github.com/uber/go-torch
  • 下载并复制flamegraph.pl(https://github.com/brendangergg/FlameGraph) 至 $GOPATH/bin路径下
  • 将$GOPATH/bin加入$PATH

4)通过文件方式输出Profile

灵活性高,适用于特定代码段的分析,通过手动调用runtime/pprof的API,API相关文档:https://studygolang.com/static/pkgdoc/pkg/runtime_pprof.htm
go tool pprof [binary] [binary.prof]go build xxx.go./xxxxgo tool pprof prof xxx.proftoplist  [function]svg 生成图go-touch [binary.prof]

5) Go支持的多种Profile

  • go help testflag
  • https://golang.org/src/runtime/pprof/pprof.go

6)通过HTTP方式输出Profile

简单,适合于持续性运行的应用,在应用程序的中导入import_ "net/http/pprof", 并启动http server即可

http://:/debug/pprof
go tool pprof _http://:/debug/pprof/profile?seconds=10 (默认为30s)go-torch -seconds 10 http://:/debug/pprof/profile

7)常见性能优化指标

  • Wall Time : 挂钟时间(函数运行的绝对时间)
  • CPU Time
  • Block Time
  • Memory allocation
  • GC times/time spent

8) 通过Benchmark进行性能分析

go test -bench=.    //benchmark测试go test -brench=. -cpuprofile=cpu.prof   //生成cpu prof分析文件go test -brench=. -memprofile=mem.prof  //生成mem prof分析文件go tool pprof mem.prof  // 性能分析

9)锁对性能的影响

(1)减少锁的影响范围
(2)减少发生锁冲突的概率
A. sync.Map
  • 场景:读多写少场景
  • 原理:https://my.oschina.net/qiangmzsx/blog/1827059
B. Concurrent Map
  • 场景:读写相当场景
  • 地址:https://github.com/easierway/concurrent_map
(3) 避免锁的使用
  • LAMX Disruptor: https://martinfowler.com/articles/lmax.html

10)字符串连接

常见字符串连接方式:
  • +
  • fmt.Sprintf
  • strings.Builder
  • bytes.Buffer
上述方式的性能:strings.Builder >= bytes.Buffer > + > fmt.Sprintf

五、 GO设计思路

1) 面向错误的设计

A. 隔离
隔离错误:Micro Kernel(微内核设计)
6e02fcf94910c31410248640c3279fa6.png
隔离错误:部署
b7c20079e20e71a5a049d96148c22f4a.png
B. 重用VS隔离
25f27090be5f49cd7eff305c7c15d282.png
C. 冗余
2033bd881a835ad57adb9065ece9c961.png
D. 单点失效
01c9ef8e157f6e581c59d891ccea142d.png
E. 慢响应
  • 不要无休止的等待(给阻塞操作都加上一下超时限制)
D. 错误传递
断路器(配合服务降级)
e60c1044f78878ed1748f5e9cc739e86.png

2) 面向恢复的设计

A. 健康检查
a. 注意僵尸进程
  • 池化资源耗尽
  • 死锁
b. let it crash!
B. 构建可恢复的系统
a. 拒绝单体系统
b. 面向错误和恢复的设计
  • 在依赖服务不可用时,可以继续存活
  • 快速启动
  • 无状态
C. 与客户端协商

3) Chaos Engineering

如果问题经常发生人们就会学习和思考解决它的方法

22a76def28843ec0fd361565bf69119f.png
A. Chaos Engineering原则
  • 构建一个我们想要的稳定的形为
  • 尝试各种真实事件
  • 在生产环境运行实验
  • 自动持续的实验
  • 最小化影响半径
B. 相关开源项目

https://github.com/Netflix/chaosmonkey

https://github.com/easierway/service_decorators/blob/master/README.md

六、GO相关书籍推荐

a948aeb65cf5e58ec3a568a4d9d29971.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值