Difference between := and = in GoLang

Hi, this is Charu from Classmethod. In this blog, we will try to understand the difference between = and := operators in Go. Go, with its simplicity and efficiency, has quickly become favourite among programmers. I have recently started learning Go and I thought only := assignment operator is used to assign any value to a variable. But they use = operator too. I got very confused in the starting, so here I am to clear your confusion with the help of this blog.

Understanding the "=" Operator:

The = operator in Go is the assignment operator. It is used to assign the value(right side) to a variable(left side).

var a int
a = 5

In this example, the value 5 is being assigned to variable a. The key point here is that a must be declared before it can be assigned a value using =.

When to Use "="?

When you have already declared a variable and want to assign or change its value. When you're working with variables that are already known to your code block.

Understanding the ":=" Operator

The := operator is known as the short variable declaration operator in Go. It's a shorthand for declaring a new variable and assigning a value to it in a single step.

b := 10

This line of code declares a new variable b, infers its type based on the assigned value (10 makes it an int), and assigns it the value 10, all in one go.

When to Use ":="?

When you want to declare a new variable and assign a value to it simultaneously. Inside functions, for concise variable initialization without specifying the type explicitly. When you want Go to infer the type of the variable based on the initial value.

Example:

Before moving further, try to figure out what is wrong with this following code-

func split(sum int) (int, int) {
    x = sum * 4 / 9  
    y = sum - x 
    return x, y
}

If you want to assign a value to a variable using = operator, that variable must be declared first. The compiler does not know the type or existence of x and y in the snippet above, hence it will throw an error.

Solution:

func split(sum int) (x, y int) {
    x = sum * 4 / 9
    y = sum - x
    return
}

Here, x and y are declared as part of the function signature itself. This means that when the function begins execution, x and y are already declared as variables of type int. Therefore, inside the function, you can directly assign values to x and y using the = operator. The := operator is not required in this context because it is used for declaring and initializing a new variable, whereas x and y are already declared.

OR

func split(sum int) (int, int) {
    x := sum * 4 / 9
    y := sum - x
    return x,y
}

Here, the variables x and y were not declared before hand, hence := operator was needed to declare and initialize the variables at once.

Conclusion

Understanding the difference between = and := in Go is crucial for writing concise and effective Go code. Here is the summary,

Declaration: = is used for assignment to already declared variables, whereas := is used for declaring and initializing a new variable.

Type Inference: With :=, Go automatically infers the variable's type based on the value assigned to it. With =, the variable's type must be explicitly declared beforehand.

Scope: := can also implicitly declare a new variable in a more limited scope. For example, inside an if statement, a variable declared with := is only accessible within that if block.

Thank you for reading!

Happy Learning:)