Skip to content

Commit 7c3491d

Browse files
ww-daniel-moraleighmcculloch
authored andcommitted
Add exception for version (#10)
### What Add exception for global variables named version. ### Why Current best practice for setting the version of a Go executable requires a global variable. This change adds a narrow exception for variables name version exactly.
1 parent abbdf6e commit 7c3491d

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ Global variables are an input to functions that is not visible in the functions
1313
https://peter.bourgon.org/blog/2017/06/09/theory-of-modern-go.html
1414
https://twitter.com/davecheney/status/871939730761547776
1515

16+
### Exceptions
17+
18+
There are very few exceptions to the global variable rule. This tool will ignore the following patterns:
19+
* Variables with an `Err` prefix
20+
* Variables named `_`
21+
* Variables named `version`
22+
1623
## Install
1724

1825
```

check_no_globals.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func isWhitelisted(i *ast.Ident) bool {
14-
return i.Name == "_" || looksLikeError(i)
14+
return i.Name == "_" || i.Name == "version" || looksLikeError(i)
1515
}
1616

1717
// looksLikeError returns true if the AST identifier starts

check_no_globals_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ func TestCheckNoGlobals(t *testing.T) {
105105
"testdata/8/code.go:30 declaredErr is a global variable",
106106
},
107107
},
108+
{
109+
path: "testdata/9",
110+
wantMessages: []string{
111+
"testdata/9/code.go:3 Version is a global variable",
112+
"testdata/9/code.go:4 version22 is a global variable",
113+
},
114+
},
108115
{
109116
path: ".",
110117
wantMessages: nil,
@@ -135,6 +142,8 @@ func TestCheckNoGlobals(t *testing.T) {
135142
"testdata/8/code.go:20 myVarError is a global variable",
136143
"testdata/8/code.go:21 customErr is a global variable",
137144
"testdata/8/code.go:30 declaredErr is a global variable",
145+
"testdata/9/code.go:3 Version is a global variable",
146+
"testdata/9/code.go:4 version22 is a global variable",
138147
},
139148
},
140149
}

testdata/9/code.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package code
2+
3+
var Version string
4+
var version22 string
5+
var version string

0 commit comments

Comments
 (0)