Go - Variables

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

The call of a variable can be composed of letters, digits, and the underscore person. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because Go is case-sensitive.Based on the basic types explained in the previous chapter. There are following basic variable types −
Sr.No Type & Description


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



The most natural length of integer for the device.



A single-precision floating point price.

Go programming language also allows to define various other types of variables such as Enumeration, Pointer, Array, Structure, and Union, which we will discuss in last chapters. In this chapter, we will show only basic variable types.

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 legitimate Go statistics type inclusive of byte, int, float32, complex64, boolean or any consumer-defined object, etc.,
var x, y, z int;
var  c, ch byte;
var  f, profits float32;
d =  forty two;
The statement “var x, y, z;” broadcasts and defines the variables x, y and z; which instructs the compiler to create variables named x, y, and z of kind int. D = 3, f = five; // statement of d and f. Here d and f are int

Mixed Variable Declaration in Go

Variables of various types can be declared in a single move the usage of kind inference.


package deal important

import "fmt"

func fundamental() 
   var a, b, c = 2, three, "jojo"  
   fmt.Printf("a is of kind %Tn", a)
   fmt.Printf("b is of type %Tn", b)
   fmt.Printf("c is of kind %Tn", c)

When the above code is compiled and executed, it produces the following result −


$cross run principal.Cross
a is of type int
b is of type int
c is of kind string

The lvalues and the rvalues in Go

There are two styles of expressions in Go
  • lvalue − Expressions that refer to a memory area is called "lvalue" expression. An lvalue may also seem as both the left-hand or right-hand side of an project.
  • 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.

There are given statement is valid −

x = 20.0

There are given statement is not valid. It would generate compile-time error −

10 = 20