工作中用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語言統一技術棧, 可以輕鬆減少大量重複性工作, 高效寫出「值得信賴」的代碼

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