Global variable not used, but it works fine

115 views
Skip to first unread message

zpsyh...@gmail.com

unread,
Apr 21, 2018, 9:30:22 AM4/21/18
to golang-nuts
#### What did you do?
https://play.golang.org/p/aryK9Btv5kH

#### What did you expect to see?
There should be a error "declared and not used"

#### What did you see instead?
It seems work fine.

#### System details

```
go version go1.10.1 windows/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="C:\Users\zps\AppData\Local\go-build"
GOEXE=".exe"
GOHOSTARCH="amd64"
GOHOSTOS="windows"
GOOS="windows"
GOPATH="C:\Users\zps\go"
GORACE=""
GOROOT="C:\Go"
GOTMPDIR=""
GOTOOLDIR="C:\Go\pkg\tool\windows_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\zps\AppData\Local\Temp\go-build846935694=/tmp/go-build -gno-record-gcc-switches"
GOROOT/bin/go version: go version go1.10.1 windows/amd64
GOROOT/bin/go tool compile -V: compile version go1.10.1
```

Tyler Compton

unread,
Apr 22, 2018, 4:33:01 AM4/22/18
to zpsyh...@gmail.com, golang-nuts
Go does not flag unused global variables, it is intended functionality. I've personally found that unused global variables have a higher chance at being benign than local variables, but there's probably an argument to be made for flagging globals, too.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vladimir Varankin

unread,
Apr 23, 2018, 5:38:05 AM4/23/18
to golang-nuts
I think it's way harder to guaranty that the globals aren't used since even non-exported functions/varibles can be used by the linker.
Reply all
Reply to author
Forward
0 new messages