Golang Tutorial Index


Every programming language needs an implementation of computer memory, i.e., RAM locations. The Go compiler uses the concept of variables to implement these, which is also used by almost all other programming languages. This tutorial will guide you on how to use variables in Go programming.



What Are Variables in Go Programming?

Variables are names given by a computer program to the memory areas that are intended for use in further operations in the program. Variables in Go are of specific types that determine the size and layout of the values, or literally, these variables will hold names (memory locations).

Variable Declaration and Initialization

In Go programming, the var keyword is used to declare and initialize variables explicitly. Here is a code snippet of declaring and initializing a Variable in Go.

Example:

/* Author: w3schools.in
Description: Example of declaring and initializing a variable in Go */

package main
import "fmt"

func main() {
    var rollno int = 205 //The integer value initialized in the variable rollno
    fmt.Println("The assigned value is", rollno)
}

Program Output:

The assigned value is 205

If you do not initialize the value, then by default, the value will be 0.

In Go programming, different data types are used to declare variables, which are explained separately in the Go Data Types tutorial.

Rules for Choosing Variable Names

  • Go variables can be named using just letters or an underscore.
  • Variable names in Go cannot start with a digit.
  • There is no limit on variable names in Go.
  • Only _ (underscore) is allowed to be used from all symbols in a variable name.
  • Go keywords cannot be used as variable names.
  • Variable names in Go are case-sensitive.
  • Using spaces in variable names is not allowed.

Multiple Variable Declaration

Go allows declaring and initializing multiple variable names on the same line. The syntax for creating multiple variable names at once is:

Syntax:

var variable_name1, variable_name2, ..., variable_nameN <data type> = value1, value2, ..., valueN

Example:

/* Author: w3schools.in
Description: Example of Go Multiple Variable Declaration */

package main
import "fmt"

func main() {
    var rollno, marks int = 205, 450
    fmt.Println("The roll number is", rollno, "and the total marks are", marks)
}

Program Output:

The roll number is 205, and the total marks are 450

Variable Assignment Without Declaring Data Type

The Go compiler is smart enough to infer the data type of a variable based on the initialization done for it. In other words, depending on the value assigned to the variable, Go understands what type of value is to be stored in that variable name.

Example:

/* Author: w3schools.in
Description: Inferring the data type of a variable based on initialization. */

package main
import "fmt"

func main() {
    var boolVal = true // This is Boolean value
    fmt.Println(boolVal)

    var str = "W3schools" // This is string value
    fmt.Println(str)
}

Program Output:

true
W3schools

Shorthand for Declaring and Initializing Variables

Instead of using the var keyword when declaring a variable, ":=" (shorthand assignment) can also be used. It is a quick programming approach.

Example:

/* Author: w3schools.in
Description: Shorthand for Declaring and Initializing Variables in Go */

package main
import "fmt"

func main() {
    boolVal := true // This is Boolean value
    fmt.Println(boolVal)

    str := "W3schools" // This is string value
    fmt.Println(str)
}


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram