OneBite.Dev - Coding blog in a bite size

assign multiple variables in Go

Code snippet on how to assign multiple variables in Go

  package main
  
  import "fmt"
  
  func main() {
    var a, b, c int
    a, b, c = 5, 6, 7
    fmt.Println(a, b, c)
  }

This code declares 3 variables (a, b and c) with the data type of integer (int). Then, it assigns different values to each of these variables in a single statement. Finally, it prints the values of the variables using the fmt.Println() method.

To declare the variables, the keyword “var” is used followed by the variables’ names (a, b and c). Then, the ”=” symbol is used along with the values that the variables will be assigned to - in this case, the values are 5, 6, and 7. Finally, fmt.Println() prints the values of the variables to the console.

go