[转]Golint代码规范检测

[原创地址]https://blog.csdn.net/chenguolinblog/article/details/90665161

一. Golint介绍

Golint is a linter for Go source code.

  1. Golint 是一个源码检测工具用于检测代码规范
  2. Golint 不同于gofmt, Gofmt用于代码格式化

Golint会对代码做以下几个方面检查

  1. package注释 必须按照 “Package xxx 开头”
  2. package命名 不能有大写字母、下划线等特殊字符
  3. struct、interface等注释 必须按照指定格式开头
  4. struct、interface等命名
  5. 变量注释、命名
  6. 函数注释、命名
  7. 各种语法规范校验等

二. Golint安装

  1. go get -u github.com/golang/lint/golint
  2. ls $GOPATH/bin (可以发现已经有golint可执行文件)

三. Golint使用

golint检测代码有2种方式

  1. golint file
  2. golint directory

golint校验常见的问题如下所示

  1. don't use ALL_CAPS in Go names; use CamelCase
    不能使用下划线命名法,使用驼峰命名法
  2. exported function Xxx should have comment or be unexported
    外部可见程序结构体、变量、函数都需要注释
  3. var statJsonByte should be statJSONByte
    var taskId should be taskID
    通用名词要求大写
    iD/Id -> ID
    Http -> HTTP
    Json -> JSON
    Url -> URL
    Ip -> IP
    Sql -> SQL
  4. don't use an underscore in package name
    don't use MixedCaps in package name; xxXxx should be xxxxx
    包命名统一小写不使用驼峰和下划线
  5. comment on exported type Repo should be of the form "Repo ..." (with optional leading article)
    注释第一个单词要求是注释程序主体的名称,注释可选不是必须的
  6. type name will be used as user.UserModel by other packages, and that stutters; consider calling this Model
    外部可见程序实体不建议再加包名前缀
  7. if block ends with a return statement, so drop this else and outdent its block
    if语句包含return时,后续代码不能包含在else里面
  8. should replace errors.New(fmt.Sprintf(...)) with fmt.Errorf(...)
    errors.New(fmt.Sprintf(…)) 建议写成 fmt.Errorf(…)
  9. receiver name should be a reflection of its identity; don't use generic names such as "this" or "self"
    receiver名称不能为this或self
  10. error var SampleError should have name of the form ErrSample
    错误变量命名需以 Err/err 开头
  11. should replace num += 1 with num++
    should replace num -= 1 with num--
    a+=1应该改成a++,a-=1应该改成a–

四. Goland配置golint

  1. 新增tool: Goland -> Tools -> External Tools 新建一个tool 配置如下:

    image.png

    参数:-set_exit_status $FilePath$

  2. 新增快捷键: Goland -> Tools -> Keymap -> External Tools -> External Tools -> golint 右键新增快捷键

  3. 使用: 打开Go文件,然后使用快捷键就可以进行代码检测了
    有golint的输出日志说明存在代码不规范的地方,需要进行修改

五. gitlab提交限制

为了保证项目代码规范,我们可以在gitlab上做一层约束限制,当代码提交到gitlab的时候先做golint校验,校验不通过则不让提交代码。

我们可以为Go项目创建gitlab CI流程,通过.gitlab-ci.yml配置CI流程会自动使用govet进行代码静态检查、gofmt进行代码格式化检查、golint进行代码规范检查、gotest进行单元测试

例如go-common项目 .gitlab.yml文件如下,相关的脚本可以查看scripts

# gitlab CI/CD pipeline配置文件
# 默认使用本地定制过的带有golint的golang镜像
image: golang:custom

stages:
    - test
    
before_script:
    - mkdir -p /go/src/gitlab.local.com/golang
    - ln -s `pwd` /go/src/gitlab.local.com/golang/go-common && cd /go/src/gitlab.local.com/golang/go-common

# test stage
# job 1 test go vet
job_govet:
    stage: test
    script:
        - bash ./scripts/ci-govet-check.sh
    tags:
        - dev
# job 2 test go fmt
job_gofmt:
    stage: test
    script:
        - bash ./scripts/ci-gofmt-check.sh
    tags:
        - dev
# job 3 test go lint
job_golint:
    stage: test
    script:
        - bash ./scripts/ci-golint-check.sh
    tags:
        - dev
# job 4 test go unit test
job_unit:
    stage: test
    script:
        - bash ./scripts/ci-gotest-check.sh
    tags:
        - dev
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 目录 统一规范篇 命名篇 开发篇 优化篇 统一规范篇 本篇主要描述了公司内部同事都必须遵守的一些开发规矩,如统一开...
    零一间阅读 6,195评论 0 2
  • Go 语言实战: 编写可维护 Go 语言代码建议 目录 1. 指导原则1.1 简单性1.2 可读性1.3 生产力...
    DukeAnn阅读 4,565评论 0 5
  • 原文链接:https://draveness.me/golang-101 Go 语言是一门简单、易学的编程语言,对...
    Draveness阅读 12,059评论 5 24
  • 本规范旨在为日常Go项目开发提供一个代码的规范指导,方便团队形成一个统一的代码风格,提高代码的可读性,规范性和统一...
    Shaw_Lee阅读 4,778评论 0 1
  • 原文链接 https://golang.org/cmd/go/由于内容过长,分两次发布 Go 是一个用来管理Go源...
    Cxb168阅读 5,040评论 0 0