Limit a variable's work scope within a go file

The scope of a variable as follows:

  1. Whthin the braces, if it is a local variable.
  2. Whthin whole package, if it define out of a pair of braces.
    However, sometimes we need an variable which just work in current go file, because I just want to use it in current go file but not pollute other go files within current package.

Just imagine such scenarios:

  1. I need a variable, and I want to share it within current go file, but want to forbidden access it out of current go file. I have no way. It broke the rule:minimize the scope of variable.
  2. A big package within many go files. I need a log variable to record log. I can define just only one log variable with the name “logger”. Even if I just want to modify some format configuration of logger and want it just work on current go file, it will influence the whole package.

If go language can provide a keyword, such as “local”, to limit a variable’s work scope within a go file, it will be very useful for go programmer.

Suggestions for language changes usually have to provide compelling arguments for why the change is necessary, has no disadvantages, works well with existing code and does not block future development.

A new keyword local will probably conflict with much existing code where local is used as a name.

If a package is so big that file local scope would be useful within the package, the less invasive action would be to reduce the since of the package.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.