• Stars
    star
    333
  • Rank 125,814 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created about 6 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

go-consistent

Build Status Go Report Card

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

Quick start / Installation

This install the go-consistent binary:

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:

go-consistent --help

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

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:

go-consistent -v ./...

Overview

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

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

// A: no parenthesis
import "fmt"

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

zero val ptr alloc

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

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

empty slice

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

// B: slice literal
[]T{}

empty map

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

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

hex lit

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

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

range check

// A: left-aligned
x > low && x < high

// B: center-aligned
low < x && x < high

and-not

// A: using &^ operator (no space)
x &^ y

// B: using & and ^ (with space)
x & ^y

float lit

// A: explicit int/frac parts
0.0
1.0

// B: implicit int/frac parts
.0
1.

label case

// A: all upper case
LABEL_NAME:

// B: upper camel case
LabelName:

// C: lower camel case
labelName:

untyped const coerce

// A: LHS type
var x int32 = 10
const y float32 = 1.6

// B: RHS type
var x = int32(10)
const y = float32(1.6)

arg list parens

// A: closing parenthesis on the same line
multiLineCall(
	a,
	b,
	c)

// B: closing parenthesis on the next line
multiLineCall(
	a,
	b,
	c,
)

non-zero length test

// A: compare as "number of elems not equal to zero"
len(xs) != 0

// B: compare as "more than 0 elements"
len(xs) > 0

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

default case order

// 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 "?"
}

More Repositories

1

go-ruleguard

Define and run pattern-based custom linting rules.
Go
785
star
2

roboden-game

An indirect control real-time strategy game about robot colonies
Go
425
star
3

goism

Not a fan of Emacs Lisp? Hack Emacs in Go!
Go
346
star
4

phpgrep

Syntax-aware grep for PHP code.
Go
236
star
5

gopherkon

Go mascot image constructor. Create your cute own gopher.
TypeScript
198
star
6

go-parsefix

Fixes simple parse errors automatically. Works great in combination with goimports.
Go
84
star
7

pathing

A very fast & zero-allocation, grid-based, pathfinding library for Go.
Go
79
star
8

go-perfguard

CPU-guided performance analyzer for Go
Go
75
star
9

ebitengine-input

A Godot-inspired action input handling system for Ebitengine
Go
70
star
10

go-jdk

Run JVM-based code in Go efficiently
Go
70
star
11

qpprof

A helper tool to work with profile.proto (pprof) files
Go
60
star
12

gocorpus

The code used to serve gocorpus application
Go
51
star
13

astnorm

AST normalization experiment
Go
45
star
14

gophers-and-dragons

Rogue-like game for Go programmers.
Go
44
star
15

gogrep

Syntax-aware Go code search, based on the mvdan/gogrep
Go
38
star
16

go-namecheck

Source code analyzer that helps you to maintain variable/field naming conventions inside your project.
Go
38
star
17

ge

ebiten-based game engine for Go
Go
32
star
18

talks

A collection of slides, notes and other related stuff from talks I have given.
27
star
19

kphp-game

Simple KPHP game, a proof of concept thing (demo)
PHP
26
star
20

decipherism-game

A puzzle game where you solve the encoding machine ciphers
Go
26
star
21

perf-heatmap

Create a heatmap index based on the profile.proto profile data
Go
24
star
22

go-benchrun

Convenience wrapper around "go test" + "benchstat".
Go
22
star
23

gdata

A gamedata package that provides convenient cross-platform storage for games
Go
21
star
24

qbenchstat

My personal, slightly improved version of benchstat utility
Go
20
star
25

parsing-and-go

Go
19
star
26

repolint

Tool to check github user/organization repositories for some simple and common issues.
Go
19
star
27

awesome-kphp

A curated list of amazingly awesome KPHP libraries, resources and software
18
star
28

ebitengine-resource

A resource manager for Ebitengine
Go
17
star
29

concat

Demo repository for habr.com article about faster Go string concatenation.
Go
17
star
30

xm

XM package provides Ebitengine-compatible mod music decoder
Go
16
star
31

uber-rules

A set of ruleguard rules that try to cover some parts of the Uber Go Style Guide (https://github.com/uber-go/guide)
Go
16
star
32

gmath

A Godot-inspired math library for Ebintengine and other Go game engines
Go
15
star
33

regex

Regular expression libraries for Go
Go
15
star
34

sinecord

Create music by the power of math!
Go
14
star
35

yaml5

YAML5 - use YAML like it's JSON5.
Go
14
star
36

quasigo

quasigo is a Go subset interpreter written in Go
Go
13
star
37

cavebots-game

My LD54 game
Go
12
star
38

ebitengine-graphics

A package implementing Graphics primitives for gscene package
Go
11
star
39

go-complex-nums-emulation

Emulating builtin complex numbers with structs of floats and measuring the results
Go
10
star
40

fileprivate

A Go linter that enforces more strict members access rules inside packages
Go
9
star
41

hello-go

Go
9
star
42

gccgo_vs_gc

Comparing GCCGO 1.8.1 (GCC 7.2) vs GC 1.8.1 (and GC 1.10) on x86 (AMD64).
Shell
9
star
43

go-contributing-ru

Go contributing related information in Russian.
Go
9
star
44

avx512test

Utility that was used to generate initial Go AVX-512 encoder test suite.
Assembly
9
star
45

inltest

Package inltest helps you to test that performance-sensitive funcs are inlineable.
Go
9
star
46

gmtk2023

A 2D real-time strategy game made for a GMTK2023 game jam
Go
8
star
47

vscode-gogrep

Structural, syntax-aware search for Go code for VS Code.
TypeScript
8
star
48

benchstat.el

Proper Emacs Lisp benchmarking made simple.
Emacs Lisp
8
star
49

KLua

KLua is a FFI-based Lua5 library that can be used in both PHP and KPHP
PHP
8
star
50

blog-src

quasilyte.github.io sources
CSS
8
star
51

bitfontier

A bitmap font maker for Go
Go
8
star
52

gsignal

A lightweight Godot/Qt inspired signals and slots for Go
Go
7
star
53

KTemplate

KTemplate is a simple text template engine for PHP and KPHP
PHP
6
star
54

vscode-perf-heatmap

TypeScript
6
star
55

gophercon2021-ruleguard

GopherCon Russia 2021 ruleguard workshop
Go
6
star
56

phpsmith

phpsmith creates random PHP and KPHP programs to test their compilers and runtimes
Go
5
star
57

pratt-parsers-go

Pratt parser implemented in Go
Go
5
star
58

KSQLite

KSQLite is a FFI-based SQLite library that can be used in both PHP and KPHP
PHP
5
star
59

quasisolar-mission

The "Quasisolar Mission" game source code
C#
5
star
60

YALWEE

System for generating JIT capable interpreters
Assembly
5
star
61

n2o.el

Nitrous - extra Emacs Lisp optimizer. Transparently makes Emacs faster.
Emacs Lisp
4
star
62

go-n2o

Go external optimizer.
Go
4
star
63

vscode-phpgrep

Structural, syntax-aware search for PHP code for VS Code.
TypeScript
4
star
64

bitsweetfont

A bitmap font for Go, made with bitfontier
Go
4
star
65

gslices

This is my own slices package for go, because I don't like the stdlib API
Go
4
star
66

regexp-lint

Code used to serve regexp-lint application
Go
3
star
67

hiddensugar-game

Go
3
star
68

http-profiling

Go
3
star
69

kphp-sdlite

Simple SDL framework for KPHP
PHP
3
star
70

ktest

Test and benchmark KPHP code
Go
3
star
71

emacs-lispeed

Lispeed = Lisp + Speed
3
star
72

phpgrep-contrib

Extra utilities and docs for the phpgrep
Shell
3
star
73

cffi

Lazy way to call CGo functions.
Go
3
star
74

textocat-php-sdk

utility for using http service
PHP
2
star
75

go-unexport

Unexport symbols from a package under a workspace automatically.
Go
2
star
76

alley-of-reading

Keeping track of books and articles I've read as well as keeping notes about some of them.
2
star
77

devtools

Shared repository for Go developer tools.
Go
2
star
78

XEDq

XEDq brings Intel XED powers into Go space.
Go
2
star
79

gnu-riscv32_ext

Extending GCC riscv32 compiler and spike emulator
Shell
2
star
80

shmup-game

Go
2
star
81

grad_work

Course/Graduate work (which is currently named as "Resembler")
C++
2
star
82

go-perftune

Helper tool for manual Go code optimization.
Go
2
star
83

gopher-arts

Gopher drawings with permissive license. Use them as you like, but preferably for a positive purpose.
2
star
84

kphp-uuid

A simple demo KPHP project
PHP
1
star
85

sonic-pi-tracks

Source code of my Sonic Pi tracks.
Ruby
1
star
86

typ.el

Type inference framework for Emacs. Build better APIs, tools, linters and optimizers with type info!
Emacs Lisp
1
star
87

stdinfo

Go
1
star
88

tquest

A text quest/dialogue tree execution engine for games written in Go
1
star
89

Perl6Scheme

Scheme subset implementation in Perl6. This is a toy.
Perl 6
1
star
90

xedmap

Mappings between XED names and terms to other widespread forms.
Go
1
star
91

Emacs-Lisp-VM

Emacs Lisp bytecode interpreter implemented in Go
Go
1
star
92

RAGF

Raaagf! Red Assembly Goez Fasta!
C++
1
star
93

gscene

A lightweight scene package for Ebitengine
Go
1
star
94

textocat-racket-sdk

Racket
1
star
95

PragMacro

C language extensions via pragma
C
1
star
96

kphp-batteries

Unofficial package that provides some PHP functions that are not implemented in KPHP stdlib.
PHP
1
star
97

ld55-game

Go
1
star
98

pprofutil

Helper functions for working with profile.proto objects
Go
1
star
99

ebitengine-sound

Audio-related helpers like playlists and sound queues
Go
1
star
100

ktemplate-playground

Sources for the KTemplate playground
TypeScript
1
star