Go - Variables

Each variable in Go has a specific type, which determines the size and layout of the variable's memory

The name of a variable can be composed of letters, digits, and the underscore character.
Sr.No Type & Description


Typically a single octet(one byte). This is an byte type.



The most natural size of integer for the machine.



A single-precision floating point value.

Variable Definition in Go

A variable definition tells the compiler where and how much storage to create for the variable. var variable_list optional_data_type; optional_data_type is a valid Go data type including byte, int, float32, complex64, boolean or any user-defined object, etc.,
var x, y, z int;
var  c, ch byte;
var  f, salary float32;
d =  42;
The statement β€œvar x, y, z;” declares and defines the variables x, y and z; which instructs the compiler to create variables named x, y, and z of type int. d = 3, f = 5; // declaration of d and f. Here d and f are int

Mixed Variable Declaration in Go

Variables of different types can be declared in one go using type inference.


package main

import "fmt"

func main() {
   var a, b, c = 2, 3, "jojo"  
   fmt.Printf("a is of type %T\n", a)
   fmt.Printf("b is of type %T\n", b)
   fmt.Printf("c is of type %T\n", c)


$go run main.go
a is of type int
b is of type int
c is of type string

The lvalues and the rvalues in Go

There are two kinds of expressions in Go
  • lvalue βˆ’ Expressions that refer to a memory location is called "lvalue" expression. An lvalue may appear as either the left-hand or right-hand side of an assignment.
  • rvalue βˆ’ The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right- but not left-hand side of an assignment.

Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side.