工作中用Go: 工具篇

工具篇 - goland

代码风格统一

action on save

Optimize import

一般会group成3-4类(上面配置了 goimports, 下面的配置可选)

  • go源码
  • 引入的pkg
  • 项目内pkg
  • _ 引入用来初始化的 pkg

before commit

码出高效: Ide feature trainer

goland 有非常多高效的操作, 强烈推荐

go struct <-> json

快速查看go源码

在对应package上使用 cmd-b

Context action(option-enter): 当前上下文支持的快捷操作

非常智能, 生产力工具

重构(ctrl-T): 代码修改推荐走重构, ide 帮助省一堆的修改

使用 test 快速验证go代码

使用 scratch 快速验证go代码

工具篇 - CI

golangci-lint

go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go run -v . # 文件/目录

一份统一的 golangci-lint 配置, 供参考

# 项目下的 .golangci.yaml 文件
# 省略其他配置, 只保留了线上开启的 lint
linters:
# Disable all linters.
  # Default: false
  disable-all: true
# Enable specific linter
  # https://golangci-lint.run/usage/linters/#enabled-by-default-linters
  enable:
    - errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
    - govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
    - ineffassign # Detects when assignments to existing variables are not used
    - staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks
    - typecheck # Like the front-end of a Go compiler, parses and type-checks Go code
    - bodyclose # checks whether HTTP response body is closed successfully
    - sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed.
    - makezero # Finds slice declarations with non-zero initial length
    - exportloopref # checks for pointers to enclosing loop variables
    #- exhaustive # check exhaustiveness of enum switch statements
    - errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.
    - nilerr # Finds the code that returns nil even if it checks that the error is not nil.
    #- contextcheck # check the function whether use a non-inherited context
    - asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers
    #- gosec # Inspects source code for security problems
    # - deadcode # Finds unused code #TODO
    # - structcheck # Finds unused struct fields
    # - unused # Checks Go code for unused constants, variables, functions and types
    # - varcheck # Finds unused global variables and constants
    # - gosimple # Linter for Go source code that specializes in simplifying a code
    # - gofmt # Linter for Go source code that specializes in simplifying a code
  # Run only fast linters from enabled linters set (first run won't be fast)
  # Default: false
  fast: false

静态检查修复建议

静态检查定义与修复建议: https://staticcheck.io/docs/checks

写在最后

得益于:

  • Go语言本身出色的工具链
  • IDE的强大支持
  • 研发效能平台助力

使用Go语言统一技术栈, 可以轻松减少大量重复性工作, 高效写出「值得信赖」的代码

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章