unexportedglobal

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2023 License: MIT Imports: 8 Imported by: 0

README

unexportedglobal

Introduction

Go Reference CI codecov

unexportedglobal is a linter for Go that verifies that all unexported global constants and variables in a Go program are prefixed with an underscore (_).

BadGood
const defaultHost = "example.com"
const _defaultHost = "example.com"
Why

The linter is inspired by the guidance, Prefix Unexported Globals with _ in the Uber Go Style Guide.

Prefixing unexported global variables with an underscore effectively places them in a namespace separate from normal variables, eliminating risk of accidentally shadowing or overwriting such a global variable.

Namespace diagram

Usage

Command line usage

To use unexportedglobal from the command line, first install the standalone program by running:

go install go.abhg.dev/unexportedglobal/cmd/unexportedglobal@latest

Then use it like so:

go vet -vettool=$(which unexportedglobal) ./...

You can also invoke it directly, although the output can be noisier in this form.

unexportedglobal ./...
Use as a golangci-lint plugin

To use unexportedglobal as a golangci-lint plugin, take the following steps:

  • Clone the repository or download a source archive from the Releases page.

    git clone https://github.com/abhinav/unexportedglobal.git
    
  • Build the plugin:

    cd unexportedglobal
    go build -buildmode=plugin ./cmd/unexportedglobal
    
  • Add the linter under linter-settings.custom in your .golangci.yml, referring to the compiled plugin (usually called 'unexportedglobal.so').

    linter-settings:
      custom:
        unexportedglobal:
          path: unexportedglobal.so
          description: Verify unexported globals have an underscore prefix.
          original-url: go.abhg.dev/unexportedglobal
    
  • Run golangci-lint as usual.

Warning: For this to work, your plugin must be built for the same environment as the golangci-lint binary you're using.

See How to add a private linter to golangci-lint for details.

FAQ

What about unexported sentinel errors?

This linter does not require you to rename sentinel error values. That is, the following will be left alone:

var errNotFound = errors.New("not found")

This aligns with the exception carved for this case in the style guide.

This exception exists because adding the _ prefix to sentinel errors would not serve any value in idiomatic Go code. It's rare to capture a returned error in a variable not named err, so there's little risk of conflict between the captured error variables and the global sentinel error variables.

License

This software is made available under the MIT License. See LICENSE for details.

Documentation

Overview

Package unexportedglobal implements a linter that verifies that unexported global variables and constants in a Go program are prefixed with '_' in their names.

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name: "unexportedglobal",
	Doc:  "requires a '_' prefix on unexported global variables and constants",
	Run:  run,
	Requires: []*analysis.Analyzer{
		inspect.Analyzer,
	},
}

Analyzer implements the unexportedglobal linter.

See package documentation for details.

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd
unexportedglobal
unexportedglobal is a linter for Go code that verifies that unexported global variables and constants are prefixed with '_' in their names.
unexportedglobal is a linter for Go code that verifies that unexported global variables and constants are prefixed with '_' in their names.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL