[Solved] Without IDE-like feature, how do we know where package-level variables are defined?


Are you using terminal with no GUI? If you do not have access to an IDE, all I can think about is using Linux command like grep to search in a lot of files inside of the package folder for var <variableName> terms.

If you have a package named test with 100 files and you want to know where boo top-level package variable is, then do:

grep -nw /path/to/test/*.go -e '^var boo'

Explanation:

  • -n to output the line number
  • -w stands for whole word
  • -e is for the pattern

Using /*.go instead of -r flag to make sure that it searches only in .go files in the top level directory, (not searching inside the sub-packages too).

WARNING: this assumes that all files are correctly formatted with gofmt for example and all package variables are not indented. If by accident the variable gets indented with spaces/tabs then you can just omit ^ in the pattern, but this will probably find var boo local variables inside functions as well.

If a package has too many package-level variables and the package is large, then it could be a problem. Top-level variables are like globals, which are better avoided unless you know what to do.

By the way, doing go build -v or running other tools with -v will output extra information like the packages that are built along. This might help you do your searching.

solved Without IDE-like feature, how do we know where package-level variables are defined?