• 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 main
    
    import (
    	"flag"
    	"fmt"
    )
    
    func init() {
    	flag.StringVar(&name, "name", "everyone", "The greeting object.")
    }
    
    var name string
    
    func 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相关库

    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信息

    (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
    ./xxxx
    go tool pprof prof xxx.prof
    top
    list  [function]
    svg 生成图
    go-touch [binary.prof]

    5) Go支持的多种Profile

    6)通过HTTP方式输出Profile

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

    http://<host>:<port>/debug/pprof
    go tool pprof _http://<host>:<port>/debug/pprof/profile?seconds=10 (默认为30s)
    go-torch -seconds 10 http://<host>:<port>/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
    B. Concurrent Map

    (3) 避免锁的使用

    10)字符串连接

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

    五、 GO设计思路

    1) 面向错误的设计

    A. 隔离

    隔离错误:Micro Kernel(微内核设计)

    隔离错误:部署

    B. 重用VS隔离

     

    C. 冗余

    D. 单点失效

    E. 慢响应

    • 不要无休止的等待(给阻塞操作都加上一下超时限制)

    D. 错误传递

    断路器(配合服务降级)

    2) 面向恢复的设计

    A. 健康检查

    a. 注意僵尸进程
    • 池化资源耗尽
    • 死锁
    b. let it crash!

    B. 构建可恢复的系统

    a. 拒绝单体系统
    b. 面向错误和恢复的设计
    • 在依赖服务不可用时,可以继续存活
    • 快速启动
    • 无状态
    C. 与客户端协商 

    3) Chaos Engineering

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

    A. Chaos Engineering原则

    • 构建一个我们想要的稳定的形为
    • 尝试各种真实事件
    • 在生产环境运行实验
    • 自动持续的实验
    • 最小化影响半径

    B. 相关开源项目

    https://github.com/Netflix/chaosmonkey

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

     六、GO相关书籍推荐

  • 相关阅读:
    java JSONObject
    android 8.0 悬浮窗 最简demo
    使用adb 命令(atrace)抓起systrace的方法。
    使用python处理selenium中的获取元素属性
    使用adb/Linux获取网关ip
    Requests text乱码
    python-uiautomator2
    adb命令 判断锁屏
    缓存穿透、缓存击穿与缓存雪崩
    ReentrantLock重入锁详解
  • 原文地址:https://www.cnblogs.com/Terry-Wu/p/12575597.html
Copyright © 2020-2023  润新知