On Tue, Mar 2, 2021 at 9:37 AM Jad <
d...@madi.se> wrote:
>
> I'm new to Go and I would like to understand why the compilation process, both the compiler and linker, does raise an error for a declared but not used variable at funcs level in order to ensure clean code while the linker does not raise this error during the build process for declared but not used variables at package level but treat them as 'dead code' and silently remove them.
>
> I asked on other mediums. Specifically Go Forum and #Go-nuts IRC and some more experienced gophers said this might constitute a bug, at least at philosophical level as it doesn't directly affect the build process.
Package scope variables can provide hooks for other packages (if the
variables are exported) or for debuggers to change program behavior.
There's no easy way for the compiler or linker to know whether this
might happen.
In particular the compiler (perhaps unfortunately) supports a
go:linkname directive that can reach into another package and refer to
a package-scope variable. This is generally not good practice but
there is existing code that does it.
Ian