Variables and Values in Go

Variables and Values in Go

·

3 min read

Values and Variables:

Everything in programming is in form of data. These datas can be of different types such as numbers, boolean values, strings or combinations.

Literals

Literals are the values that are written in the program as it is. For example fmt.Println("Hello World!"), gives output as Hello world. The output is displayed without assigning any variable to it. You can only perform arithmetic operations on literals. For example, fmt.Println(2*3) gives you the answer 6.

Constants

Constants can be declared using the const keyword. Constant are declared to a variable so that the value does not change throughout the program.

Basic data types

There are three basic data types in Go. There are int, float and complex. Example of int is 20, -10, 898. Example of float is 89.77 and -0.99. Example of complex os 3i and 78i.

Depending upon the type of architecture, the data types can be:

uint8-Unsigned 8-bit integers (0 to 255)

uint16-Unsigned 16-bit integers (0 to 65535)

uint32-Unsigned 32-bit integers (0 to 4294967295)

uint64-Unsigned 64-bit integers (0 to 18446744073709551615)

int8-Signed 8-bit integers (-128 to 127)

int16-Signed 16-bit integers (-32768 to 32767)

int32-Signed 32-bit integers (-2147483648 to 2147483647)

int64-Signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32-IEEE-754 32-bit floating-point numbers

float64-IEEE-754 64-bit floating-point numbers

complex64-Complex numbers with float32 real and imaginary parts

complex128-Complex numbers with float64 real and imaginary parts

Error display in the console

The errors can be easily be identified with the name of the file where something went wrong, the line number in that file where Go noticed an issue, and the column number (the number of characters from the left) on that line where the error occurred. These are displayed on the console window.

Assigning values to variables:

You can values to a variable by using "=" operator. The variable will be in the left side of the operator and the value will on the right. For example, noOfStudents=5

Strings

A string is nothing but a sequence of characters. These sequence of characters can be assigned to a variable which is declared as string. For example, var name string name="Virat" Two strings can be joined using the "+" operator. This is known as concatenation. For example, firstName="Virat" secondName="Kohli" playerName=firstName+" "+secondName

Default values of each data type:

int->0 float->0 boolean(bool)->false

You can also assign values to a variable without declaring its type using the ":=" operator. You can use operator is you know the type of the value that you are going to assign to the variable. For example, days=:8

You can also declare multiple variables in a single line. You can either decalare it by

var name1,name2 string

and assign values to the variables separately

name1="Virat" name2="Kohli"

Or

you can also assign the values of the variables directly like, player1,player2:="Rohit","Dhoni"