马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

Go 零基础 30 min 入门

[复制链接]
查看2434 | 回复0 | 2019-10-24 09:48:22 | 显示全部楼层 |阅读模式
不知不觉用 Go 开辟也两年多了. 筹备点履历汇总, 方便后面的同学能快速上手.
提要
1. Go 安装 2. Go ide 搭建 3. Go modules 模块管理 4. Go unit test 5. Go debug 调试 6. Go pprof 火焰图 7. Go online 调试 8. Go future 思考 1. Go 安装
Golang 官网 https://golang.org/ Download 页面 https://golang.org/dl/ Install 页面 https://golang.org/doc/install
登录 Golang 官网. 假如访问有标题, 请激发这辈子最大潜力去深入突破此间魔障.
别问 问 就是不合适
别多想 想 就是没缘分
然后[2019/10/21]点到下载页面, 这里下载的是 go1.12.12.linux-amd64.tar.gz 随后按照安装页面提示操纵. 这里简单梳理以下脚本
  1. sudo rm -rf /usr/local/go
  2. sudo tar -C /usr/local -xzf go1.12.12.linux-amd64.tar.gz
  3. sudo vi /etc/profile
  4. Shift + G
  5. i
  6. export PATH=$PATH:/usr/local/go/bin
  7. ~
  8. wq!
  9. source /etc/profile
复制代码
通过 go version 查察安装版本
094822r00yq0qr00gqg2pl.png
原本是想通过 window 平台带大家演示一遍提要中内容. 不过常常待在 ubuntu 旁边, 只能顺手用 linux 环境给同学演示一遍. 故意人可自行选择环境实操, 思路都差不多.
2. Go ide 搭建
Download VSCode https://code.visualstudio.com/
这里推荐 VSCode 搭建 Golang 开辟换机, 下载 deb 包并安装.
  1. sudo dpkg -i code_1.39.2-1571154070_amd64.deb
复制代码
筹备下面环境
094823i0yzfyaahumuf000.png
点击 [Install] -End-> [F1] -> [Go: Install/Update Tools] -> [全选] -> [确定]
094823a3jb14ssjjmm51pq.png

大概齐都乐成后, 对着项目来下 [F5] 跑起来
094824ibwgglftszm7egse.png
别惊奇, 咱们的 IDE 已经 OK 了!
3. Go modules 模块管理
Go modules 是 Go 解决包依靠管理方面特别棒的一次实验. 这里带大家简单先用起来. 故意的同学可以查询更多的相关资料, 大概通过 go mod help 自行晋升. 开始准备素材
code/arithmetic/div.go
  1. package arithmetic
  2. import "errors"
  3. // Div 除法操纵
  4. func Div(a, b int) (c int, err error) {
  5. if b == 0 {
  6. err = errors.New("divisor is 0")
  7. return
  8. }
  9. c = a / b
  10. return
  11. }
复制代码
code/main.go
  1. package main
  2. import (
  3. "fmt"
  4. "code/arithmetic"
  5. )
  6. func main() {
  7. println("Hello, 世界")
  8. c, err := arithmetic.Div(1, 0)
  9. fmt.Printf("c = %d, err = %+v\n", c, err)
  10. }
复制代码
094825o2hytach6avcqygo.png
有了上面这些, 开始着手构建
  1. go mod init code
  2. go build
  3. ./code
复制代码
094826hw343feswkjj7jk1.png
越纯粹越懂本身
4. Go unit test
code/arithmetic/div_test.go
  1. package arithmetic
  2. import (
  3. "testing"
  4. "math/rand"
  5. )
  6. func TestDiv(t *testing.T) {
  7. var (
  8. a, b, c int
  9. err error
  10. )
  11. a, b = 1, 2
  12. c, err = Div(a, b)
  13. t.Logf("a = %d, b = %d, c = %d, err = %+v", a, b, c, err)
  14. a, b = 1, 0
  15. c, err = Div(a, b)
  16. t.Logf("a = %d, b = %d, c = %d, err = %+v", a, b, c, err)
  17. }
  18. func BenchmarkDiv(b *testing.B) {
  19. for i := 0; i < b.N; i++ {
  20. Div(rand.Int(), rand.Int())
  21. }
  22. }
复制代码
Go 中单元测试好简单, 特殊一点要求就是定名, 总结起来有下面几小点 1. 文件命必须是 *_test.go 2. 测试函数 func Test*(t *testing.T) { ... } 3. 性能测试函数 func Benchmark*(b *testing.B) { for i := 0; i < b.N; i++ { ... } }
094826xc3kvlnigl5vl3n3.png
有测试比没有要好很多 /(ㄒoㄒ)/~~
5. Go debug 调试
别深究 深究 就是 F5 F10 F11 go -race
094826rw486f65h84w7457.png

6. Go pprof 火焰图
筹备素材
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. "net/http"
  7. _ "net/http/pprof"
  8. "code/arithmetic"
  9. )
  10. func main() {
  11. println("Hello, 世界")
  12. c, err := arithmetic.Div(1, 0)
  13. fmt.Printf("c = %d, err = %+v\n", c, err)
  14. for i := 0; i < 1000000; i++ {
  15. go func() {
  16. time.Sleep(time.Second * 10)
  17. }()
  18. }
  19. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {})
  20. err = http.ListenAndServe(":8088", nil)
  21. if err != nil {
  22. log.Fatalf("ListenAndServe: %+v\n", err)
  23. }
  24. }
复制代码
启动服务, 查察程序运行状态
  1. go pprof debug http://127.0.0.1:8088/debug/pprof/
复制代码
094827vidr273dddry21lz.png

更加详细的可以一块查察一下 cpu 运行火焰图
  1. go tool pprof http://127.0.0.1:8088/debug/pprof/profile -seconds 10
复制代码
随后会生成一个 profile 相关文件, 用工具打开 pprof.code.samples.cpu.001.pb.gz
  1. go tool pprof -http=:8081 /home/zhi/pprof/pprof.code.samples.cpu.001.pb.gz
复制代码
094827kxmlb6oo6mxmlyca.png
全有了, 是否为虎傅翼不知道, 但知道, 你应对标题标方式方法会多一些.
7. Go online 调试
delve https://github.com/go-delve/delve
抛开看日志, 也可以在服务摘除后, 通过 delve 举行 b r n c ...
094828e13v80rkfuzfkndy.png
佛度有缘人
8. Go future 思考
Go 很多理念都很平凡, 实现方面也很弃取, 没想到末了居然云云的着实, 开辟工程杠杠的. 个人觉得其一大亮点是为实际生产的工程师服务, 其次为呆板服务, 合理的解决二者的痛点.
关于 Go 未来的思考, 很等候泛型模式的到来.   

  







来源:https://www.cnblogs.com/life2refuel/p/11729063.html
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则