SoFunction
Updated on 2025-03-04

Detailed explanation of golang type inference and variable redeclaration

Type inference is a programming language's ability to automatically interpret expression types during compilation.

What benefits can language type inference bring?

When writing code, we save the number of taps by using Go type inference, and the saved keyboard taps can be almost ignored. But its real benefits are often reflected in the things we have after we write the code, such as code reconstruction.

We still assign values ​​to a function by calling a function while declaring the name variable, but this function is not, but a function defined by ourselves, such as getTheFlag.

package main
import (
    "flag"
    "fmt"
)
func main() {
    var name = getTheFlag()
    ()
    ("Hello, %v!\n", *name)
}
func getTheFlag() *string {
    return ("name", "everyone", "The greeting object.")
}

We can use the getTheFlag function to wrap (or wrap) the call to the function and directly use the result as the result of the getTheFlag function. The result type is *string.

In this way, the expression on the right of var name = can be changed to a calling expression for the getTheFlag function. This is actually a refactoring of "that line of code that declares and assigns the name variable".

We usually call the code modification method that does not change any interaction mode and rules between a program and the outside world, but only changes its internal implementation" a reconstruction of the program. The refactored object can be a line of code, a function, a functional module, or even a software system.

You will find that you can change the internal implementation of the getTheFlag function and its type of return result at will without modifying any code in the main function.

This command source code file can still be compiled, and there will be no problems with building and running. Maybe you can feel that this is a qualitative change in program flexibility.

We do not explicitly specify the type of the variable name so that it can be assigned a value of any type. That is, the type of the variable name can be dynamically determined by other programs when it is initialized.

After you change the result type of getTheFlag function, the Go compiler will automatically update the type of the variable name when you build the program again.

Go is statically typed, so once its type is determined when initializing a variable, it is impossible to change it again. This avoids some problems when maintaining the program later. Also, remember that this type of determination is done during the compilation period and therefore does not have any impact on the operational efficiency of the program.

What benefits can Go type inference bring?

Answer: Go language type inference can significantly improve program flexibility, making code reconstruction easier, without causing additional burden on code maintenance (in fact, it can avoid shotgun code modification), and will not lose program operation efficiency.

2. What does the redeclaration of a variable mean?

Related to short variable declarations. By using it, we can redeclare variables in the same code block.

In Go language, code blocks are generally an area enclosed in curly braces, which can contain expressions and statements. The Go language itself and the code we write together form a very large code block, also called a full-domain code block. This is mainly reflected in that any code can be used by any open global variable. A relatively smaller code block is a code package. A code package can contain many subcode packages, so such a generation
The code block can also be large.

Next, each source file is also a code block, each function is also a code block, and each if statement, for statement, switch statement and select statement are a code block. Even case clauses in switch or select statements are independent code blocks. To the extreme, will I write a pair of curly braces next to each other in the main function be considered a code block? Of course, this is even a noun, called "empty code block".

Prerequisites for variable redeclaration:

  1. Since the type of the variable is determined at its initialization, the type assigned to it when it is declared again must be the same as
    The original type is the same, otherwise a compilation error will occur.
  2. Redeclaration of variables can only occur in a certain code block. If the current variable is duplicated in the outer code block
    variables, then there is another meaning.
  3. Redeclaration of variables only occurs when using short variable declarations, otherwise it cannot be compiled. If you want to be here
    Declare a brand new variable, then you should use a declaration statement containing the keyword var, but it cannot be the same as
    Any variables in the code block have a duplicate name.
  4. The variables "declared and assigned" must be multiple, and at least one of them is a new variable. Only then can we
    In other words, the old variables are redeclared.

From this point of view, variable redeclaration is actually a syntactic sugar (or convenience measure). It allows us to ignore whether the multiple variables are assigned to contain old variables when using short variable declarations. It can be imagined that if you don’t do this, you will write a lot of more code.

var err error
n, err := (, "Hello, everyone!\n")

I used short variable declaration to "declare and assign values" to the new variable n and the old variable err, which is also a redeclaration to the latter.

This is the end of this article about detailed explanations of golang type inference and variable redeclaration. For more related golang type inference and variable redeclaration, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!