SoFunction
Updated on 2025-03-04

Solve golang's pitfalls about global variables

Shortly after learning golang, I encountered a pitfall when defining global variables. Write a small example to enhance memory.

Error version

var p int
func main() {
 p, err := test(4)
 if err != nil {
  (err)
 }
}
func test(i int) (int, error) {
 return i + 1, nil
}

The compilation has not been passed, p declared and not used. Later, I checked the information and saw that this type of p was actually redefined in main, so I kept reminding that p was defined but was not used. Modifications are as follows:

Correct version

var p int
func main() {
 var err error
 p, err = test(4)
 if err != nil {
  (err)
 }
}
func test(i int) (int, error) {
 return i + 1, nil
}

Supplement: Golang variable scope problem - Avoid using global variables

Recently I encountered a problem with variable scope, a relatively low-level problem. As a skilled person, you should not make such low-level mistakes, but the syntax characteristics of golang may make you fall into a trap if you are not careful, hehe.

Variable scope

The scope of a global variable is the entire package, and the scope of a local variable is in the curly braces where the variable is located, which is a very basic problem.

We usually use golang's syntax sugar:= to assign values ​​to variables. This method can save us the code that defines variables and make the code more concise. However, if you define a global variable and accidentally assign values ​​to it with :=, some problems will arise.

question

Looking at the code below, it defines a global variable t. I want to assign it to 2 in init() and then use it in main.

var t int
func init() {
    t, err := ("2")
    if err != nil {
        (err)
    }
    ("init:", t)
}
func main() {
    ("main:", t)
}

Output:

init: 2

main: 0

After execution, different numbers are printed in init and main. Why are they different? Maybe you will know the reason after a closer look. It is very simple. t in init is generated with :=, so t is a local variable, and the global variable t is overwritten in the init function. The global variable t is not assigned, it is still the original value of 0.

I originally wanted to assign a value to the global variable t in init, but accidentally created a local variable with :=, which caused the global variable t to be assigned successfully, and I made a low-level mistake.

solve

After knowing the reason, it will be easy to solve. I just don't use :=. The code is as follows:

var t int
func init() {
    var err error
    t, err = ("2")
    if err != nil {
        (err)
    }
    ("init:", t)
}
func main() {
    ("main:", t)
}

Output:

init: 2

main: 2

After using :=, t in init is the global variable t, assigning the global variable t to 2, and the output of 2 in main is naturally 2, which achieves my original purpose.

think

This problem is very simple and low-level, but a golang expert may inadvertently make such a mistake in actual projects where the code is much more complicated than demo.

It is hard to guarantee that this problem will not happen next time, unless you do not use global variables or use := syntax sugar like this.

My advice is this:

Use global variables as little as possible.

Use as little as possible:= syntactic sugar.

When using :=, make sure that the lvalue has not been defined.

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.