Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/leighmcculloch/gochecknoglobals
Check that no globals are present in Go code.
https://github.com/leighmcculloch/gochecknoglobals
Last synced: 2 months ago
JSON representation
Check that no globals are present in Go code.
- Host: GitHub
- URL: https://github.com/leighmcculloch/gochecknoglobals
- Owner: leighmcculloch
- License: mit
- Created: 2018-05-21T05:42:10.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-06-17T16:50:03.000Z (12 months ago)
- Last Synced: 2024-01-30T22:51:16.216Z (4 months ago)
- Language: Go
- Size: 39.1 KB
- Stars: 99
- Watchers: 5
- Forks: 16
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Lists
- static-analysis - gochecknoglobals
README
# gochecknoglobals
[![test](https://github.com/leighmcculloch/gochecknoglobals/actions/workflows/build.yml/badge.svg)](https://github.com/leighmcculloch/gochecknoglobals/actions/workflows/build.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/leighmcculloch/gochecknoglobals)](https://goreportcard.com/report/github.com/leighmcculloch/gochecknoglobals)Check that no globals are present in Go code.
## Why
Global variables are an input to functions that is not visible in the functions signature, complicate testing, reduces readability and increase the complexity of code.
https://peter.bourgon.org/blog/2017/06/09/theory-of-modern-go.html
https://twitter.com/davecheney/status/871939730761547776### Exceptions
There are very few exceptions to the global variable rule. This tool will ignore the following patterns:
* Variables with an `err` or `Err` prefix that implement the `error` interface
* Variables named `_`
* Variables named `version`
* Variables assigned from `regexp.MustCompile()`
* Variables with a `//go:embed` comment## Install
```
go install 4d63.com/gochecknoglobals@latest
```## Usage
The linter is built on [Go's analysis package] and does thus support all the
built in flags and features from this type. The analyzer is executed by
specifying packages.[Go's analysis package]: https://pkg.go.dev/golang.org/x/tools/go/analysis
```
gochecknoglobals [package]
``````
gochecknoglobals ./...
```By default, test files are checked but can be excluded by adding the
`-test=false` flag.```
gochecknoglobals -test=false [package]
```