百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

go-consistent

> 开发工具
开源

源代码分析器,可帮助您使 Go 程序更加一致。

363 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

源代码分析器,可帮助您使 Go 程序更加一致。

go-consistent

Source code analyzer that helps you to make your Go programs more consistent.

Quick start / Installation

This install the go-consistent binary:

bash
go install github.com/quasilyte/go-consistent

If go install location is under your system $PATH, go-consistent command should be available after that.

This should print the help message:

bash
go-consistent --help

You can pass package names and separate Go filenames to the go-consistent tool:

bash
go-consistent foo.go bar.go mypkg

You can also use std, ./... and other conventional targets that are normally understood by Go tools.

  • If you want to check consistency of a single file or package, just provide their name
  • If you want to check the whole project, you should pass all its packages as an arguments

To check the entire project, run go-consistent like this:

bash
go-consistent -v ./...

Overview

To understand what go-consistent does, take a look at these 3 lines of code:

go
lit1 := map[K]V{}
lit2 := map[K]V{}
m := make(map[K]V)

lit1, lit2 and m are initialized to an empty, non-nil map. The problem is that you have at least 2 ways to do it:

  1. lit1 and lit2 use the first option, the map literal
  2. m uses the second option, the make function

Neither of these are the "best", but on the package or project level, you might want to prefer only one of them, for consistency reasons.

go-consistent tool detects that map literal used more frequently (2 vs 1) in the example above, so it suggest you to replace m initialization expression to use map literal instead of make function.

There are many similar cases where you have 2 or more options of expressing the same thing in Go, go-consistent tries to handle as much patterns as possible.

Project traits

  • Zero-configuration. Defaults should be good enough for most users. Other configuration is performed using command line arguments.
  • Can handle projects of any size. This means that there should be no significant memory consumption growth with the increased number of packages being checked. There can be "fast, but memory-hungry" option that can work best for small-average projects, but it should be always possible to check huge projects on the developer machine.

Complete list of checks performed

Checkers that require types info:

  1. zero val ptr alloc
  2. empty slice
  3. empty map
  4. untyped const coerce
  5. non-zero length test

Checkers that do not require types info:

  1. unit import
  2. hex lit
  3. range check
  4. and-not
  5. float lit
  6. label case
  7. arg list parens
  8. default case order

unit import

go
// A: no parenthesis
import "fmt"

// B: with parenthesis
import (
	"fmt"
)

zero val ptr alloc

go
// A: new call
new(T)
new([]T)

// B: address of literal
&T{}
&[]T{}

empty slice

go
// A: make call
make([]T, 0)

// B: slice literal
[]T{}

empty map

go
// A: make call
make(map[K]V)

// B: map literal
map[K]V{}

hex lit

go
// A: lower case a-f digits
0xff

// B: upper case A-F digits
0xFF

range check

go
// A: left-aligned
x > low && x  0

// C: compare as "at least 1 element"
len(xs) >= 1

default case order

go
// A: default case is the first one
switch {
default:
	return "?"
case x > 10:
	return "more than 10"
}

// B: default case is the last one
switch {
case x > 10:
	return "more than 10"
default:
	return "?"
}

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Gobest-practicesconsistencyconsistency-checkingdev-tools

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类开发工具
定价开源

> 相关工具

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具