goland 高效开发golang的配置

1. 配置Goland项目

为了达到项目的代码规范化建议设置 go fmtgoimports 的 File Watcher ,有顺序

  1. go fmt

  2. goimports

goimports 需要自己手动下载

官方:

go install golang.org/x/tools/cmd/goimports@latest

修改后的地址:

https://github.com/workwb/tools/releases/download/v0.1.11/goimports_windows.zip

修改后的导入包的方式只会分为 官方包 和 三方包

例如:

before:

import (
    "time"
    "github.com/ywanbing/confg" 

    "github.com/ywanbing/log"
)

官方 goimports 后:

import (
    "time"
    
    "github.com/ywanbing/confg" 

    "github.com/ywanbing/log"
)

我自己修改的 goimports 后:

import (
    "time"
    
    "github.com/ywanbing/confg" 
    "github.com/ywanbing/log"
)

2. 配置 goland 的方法注释

    安装goland 插件: goanno

    在图中设置模板:

Normal MethodInterface Method 中 设置如下模板

// ${function_name}
// 		注释${todo}
// 参数:
// 		${params}:
// 返回:
// 		${return_types}:

在方法的上面一行使用快捷键:

Crtl + Alt + /

可生成对应的方法注释

3. 配置 golang 的格式规范

安装插件 Go linter 

在 设置 ->  工具 -> Go Linter  中设置 配置文件的路径

 配置文件的内容我使用的已经设置好:

# This file contains all available configuration options
# with their default values.

# options for analysis running
run:
  # default concurrency is a available CPU number
  concurrency: 8

  # timeout for analysis, e.g. 30s, 5m, default is 1m
  timeout: 1m

  # exit code when at least one issue was found, default is 1
  issues-exit-code: 1

  # include test files or not, default is true
  tests: true

linters:
  disable-all: true
  enable:
    - errcheck
    - gosimple
    - govet
    - staticcheck
    - typecheck
    - bidichk
    - bodyclose
    - errname
    - funlen
    - goconst
    - gocritic
    - godox
    - gomnd
    - importas
    - makezero
    - nestif
    - revive
    - unconvert
    # Whitespace Linter - Forces you to use empty lines!
    - wsl

# all available settings of specific linters
linters-settings:
  errcheck:
    # report about not checking of errors in type assertions: `a := b.(MyStruct)`;
    # default is false: such cases aren't reported by default.
    check-type-assertions: false

    # report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
    # default is false: such cases aren't reported by default.
    check-blank: true

    # list of functions to exclude from checking, where each entry is a single function to exclude.
    # see https://github.com/kisielk/errcheck#excluding-functions for details
    exclude-functions:
      - io/ioutil.ReadFile
      - io.Copy(*bytes.Buffer)
      - io.Copy(os.Stdout)
  gosimple:
    # Select the Go version to target. The default is '1.13'.
    go: "1.16"
    # https://staticcheck.io/docs/options#checks
    checks: [ "all" ]

  govet:
    # settings per analyzer
    settings:
      printf: # analyzer name, run `go tool vet help` to see all analyzers
        funcs: # run `go tool vet help printf` to see available settings for `printf` analyzer
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
          - (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
  staticcheck:
    # Select the Go version to target. The default is '1.13'.
    go: "1.16"
    # https://staticcheck.io/docs/options#checks
    checks: [ "all" ]
  funlen:
    lines: 100
    statements: 60
  goconst:
    # minimal length of string constant, 3 by default
    min-len: 2
    # minimum occurrences of constant string count to trigger issue, 3 by default
    min-occurrences: 2
    # ignore test files, false by default
    ignore-tests: true
  gocritic:
    # 功能非常之多 https://go-critic.com/overview
    # 这里打开了所有的检查(通过标签的方式打开的)
    enabled-tags:
      - diagnostic
      - experimental
      - opinionated
      - performance
      - style
    disabled-checks:
      - commentedOutCode
      - evalOrder
      # 目前还存在问题,暂时不会使用该检查
      - dupImport # https://github.com/go-critic/go-critic/issues/845
      - ifElseChain
      - whyNoLint
      - unnecessaryBlock
  godox:
    # report any comments starting with keywords, this is useful for TODO or FIXME comments that
    # might be left in the code accidentally and should be resolved before merging
    keywords: # default keywords are TODO, BUG, and FIXME, these can be overwritten by this setting
      - NOTE
      - OPTIMIZE # marks code that should be optimized before merging
      - HACK # marks hack-arounds that should be removed before merging
      - todo
  gomnd:
    settings:
      mnd:
        # don't include the "operation" and "assign"
        checks: argument,case,condition,return
        ignored-numbers: 0,1,2,3,4,5,6,7,8,9
        ignored-functions: strings.SplitN
  importas:
    # if set to `true`, force to use alias.
    no-unaliased: true
    # List of aliases
    alias:
      # using `servingv1` alias for `knative.dev/serving/pkg/apis/serving/v1` package
      #- pkg: knative.dev/serving/pkg/apis/serving/v1
      #  alias: servingv1
      # using `autoscalingv1alpha1` alias for `knative.dev/serving/pkg/apis/autoscaling/v1alpha1` package
      #- pkg: knative.dev/serving/pkg/apis/autoscaling/v1alpha1
      #  alias: autoscalingv1alpha1
      # You can specify the package path by regular expression,
      # and alias by regular expression expansion syntax like below.
      # see https://github.com/julz/importas#use-regular-expression for details
      #- pkg: knative.dev/serving/pkg/apis/(\w+)/(v[\w\d]+)
      #  alias: $1$2
  nestif:
    # minimal complexity of if statements to report, 5 by default
    min-complexity: 4


  revive:
    # see https://github.com/mgechev/revive#available-rules for details.
    ignore-generated-header: true
    # Disable all available rules
    DisableAllRules: true
    severity: warning
    # https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md
    rules:
      - name: duplicated-imports
        Enable: true
      - name: early-return
        Enable: true
      - name: confusing-naming
        Enable: true
      - name: confusing-results
        Enable: true
      - name: error-naming
        Enable: true
      - name: error-return
        Enable: true
      - name: error-strings
        Enable: true
      - name: errorf
        Enable: true
      - name: string-of-int
        Enable: true
      - name: argument-limit
        Enable: true
        Arguments: 5


  # wsl 全部配置已完成
  wsl:
    # See https://github.com/bombsimon/wsl/blob/master/doc/configuration.md for
    # documentation of available settings. These are the defaults for
    # `golangci-lint`.
    # Controls if the checks for slice append should be "strict" in the sense that it will only allow these assignments to be cuddled with variables being appended.
    # Default value: true
    strict-append: true
    # Controls if you may cuddle assignments and calls without needing an empty line between them. Default value: true
    allow-assign-and-call: true
    # Controls if you may cuddle assignments and anything without needing an empty line between them. Default value: false
    allow-assign-and-anything: true
    # Controls if you may cuddle assignments even if they span over multiple lines. Default value: true
    allow-multiline-assign: true
    # This option allows whitespace after each comment group that begins a block. Default value: false
    allow-separated-leading-comment: false
    # Can be set to force trailing newlines at the end of case blocks to improve readability. If the number of lines (including comments)
    # in a case block exceeds this number a linter error will be yielded if the case does not end with a newline.
    # Default value: 0 (never force)
    force-case-trailing-whitespace: 0
    # Controls if you're allowed to cuddle multiple declarations. This is false by default to encourage you to group them in one var block.
    # One major benefit with this is that if the variables are assigned the assignments will be tabulated.
    #Default value: false
    allow-cuddle-declarations: true
    # Controls if you may end case statements with a whitespace. This option is independent of other blocks and
    # was introduced to improve readability for complex blocks. Default value: false
    allow-case-trailing-whitespace: false
    # Controls if blocks can end with comments. This is not encouraged sine it's usually code smell but might be useful
    # do improve understanding or learning purposes. To be allowed there must be no whitespace between the comment and
    # the last statement or the comment and the closing brace. Default value: false
    allow-trailing-comment: false
    # Enforces that an if statement checking an error variable is cuddled with the line that assigned that error variable. Default value: false
    enforce-err-cuddling: true
    # Enforces that an assignment which is actually a short declaration (using :=) is only allowed to cuddle with other short declarations,
    # and not plain assignments, blocks, etc. This rule helps make declarations stand out by themselves, much the same as grouping var statements.
    # Default value: false
    force-short-decl-cuddling: false


 

 


版权声明:本文为billgates_wanbin原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。