Tutorial GoLang

Basics: Types, Variables and Constants


In today’s post we will learn basics of GO language like supported Types, how to declare and initialize variables and constants. Just like any other language, GO has its own way of declaring and initializing variables and constants.

Data Types

  1. Boolean: Used for true and false. Use bool to declare variable of type Boolean.
  2. Numeric: Go supports unsigned, signed integer, float, complex, byte and rune
Numeric Types Description
Unsigned integers  

uint8 0-255
uint16 0-65535
uint32 0- 4294967295
uint64 0- 18446744073709551615

 

 

Signed integers  

int8 -128-127
int16 -32768-32767
int32 -2147483648-2147483647
int64 -9223372036854775808-9223372036854775807

 

 

Floating Numbers  

float32 32 bit
float64 64 bit

 

Floating Number with imaginary part  

complex64 32 bit
complex128 64 bit

 

 

byte Alias to uint8
rune Alias to int32

 

In addition to above, GO also has uint and int (32 or 64 bit depending on underlying OS) and uintptr (unsigned integer to store uninterrupted bits of pointer value)

Note: Conversions are required while using different numeric types in an expression or assignment.

  1. String: Used for representing string values. GO also defines string has immutable.

Apart from this defined types GO, also allows derived types like struct, slices, etc. We will look into them little later.

Variables

Now let us look at how we can declare and initialize variables. If you are developer form OO oriented world and use languages like Java or .Net you will find this quite different.

Let us look at an example:

func main() {
	var x int
	x = 10
	fmt.Println("Value of x :", x)
}

Here we have declared variable x of type int and on next line we are assigning value 10 to it. But this not the ideal way in which we will declare and assign value to variable. Typically we declare and initialize variable in single statement as shown below.

var x int = 10

But in GO, we have one more way to do this, and the most widely used way of declaring and assigning an initial value to a variable:

x := 10

Key thing to note here is the use of := (colon followed with equal sign). This way of declaring variable is called short hand variable.

Since GO, is statically typed language at compile time it will identify the type of value in this case an int and since we have used :=, compiler will automatically consider x as of type int

Note: Short Hand variable (:=) is only allowed within function. One cannot use := for global variables

This is not allowed

p := 0                                                 #A

func main() {
	x := 10                                            #B
	fmt.Println("Value of x : ", x)
	fmt.Println("Value of p : ", p)
}

#A : Not allowed since it is declared at a global level i.e. outside the function main
#B : Allowed since the variable is declared locally inside the function main

Allowed

var p int

func main() {
	x := 10
	fmt.Println("Value of x : ", x)
	fmt.Println("Value of p : ", p)
}

This will print:

Value of x :  10
Value of p :  0

http://play.golang.org/p/PS7GJhYxyc

Let us look at one more example

var p int

func main() {
	x := 10
	p := 100
	fmt.Println("Value of x : ", x)
	fmt.Println("Value of p in main : ", p)
	printP()
}

func printP() {
	fmt.Println("Value of p in func : ", p)
}

Output:

Value of x :  10
Value of p in main : 100
Value of p in func :  0 

http://play.golang.org/p/4d_E34uQ8P

As you can see from output, value of variable p is 100 in function main and 0 when printed via function printP and reason is in function main we have declared short hand variable p with value 100 and scope of this variable is function main, where as in printP function, the variable p from global scope is used (which is not initialized) hence 0 is printed.

GO, also supports declaring multiple variables same or different types in one line as shown below:

var p, q int

func main() {
	x, p := 10, 100.01
	fmt.Println("Value of x : ", x)
	fmt.Println("Value of q : ", q)
	fmt.Println("Value of p in main : ", p)
	printP()

}

func printP() {
	fmt.Println("Value of p in func : ", p)
}

Output:

Value of x :  10
Value of q :  0
Value of p in main :  100.01
Value of p in func :  0

http://play.golang.org/p/iIpQwDbaZd

As seen above we have declared variables p and q of type int. Similarly in the function main on 1st line, we have declared variables x and p and initialized them with 10 and 100.01 respectively. So in this case x is of type int, while p of type float

Unlike p and q above, in case if we need to declare multiple variables of different types one way to do so is to declare it like

var p int
var q string

But in Go, we can do it as follows:

var (
	p int
	q string
)

Similarly, you can initialize them, as follows:

var (
	p = 100
	q = "example"
)

Constants

They are special variables, which once initialized, their values cannot be changed. GO has a keyword const for declaring a constant variable.

const x = 100

Here we have declared, constant x and initialized it to 100. Note here we have not used :=, but still x will be of type int.

Let us look at some code below:

const x = 100

func main() {
	const y = 200.01
	fmt.Println("Value of constant x : ", x)
	fmt.Println("Value of constant y : ", y)
}

Output:

Value of constant x :  100
Value of constant y :  200.01

http://play.golang.org/p/3X4MPYm4U9

We can also declare a constant within the function, and its scope will be within that function.

Important: GO does not have keywords like public, private etc. Instead GO follow different conventions for declaring public and private variables. If name of a variable starts with an Upper Case letter, it is considered as public and if not than it is considered to be private. And this is true for all variables, functions, structure etc

If you carefully observe, the code used for printing statement on the console:

fmt.Println(“…..”)

fmt” is package name, and we are invoking the method Println. Since it starts with uppercase, it as become a public method and is available for use outside the package.

Conclusion

Today, we learned basics of Go programming like data types, declaring variables and constants. There are different conventions for doing things in GO, compared to other widely used OO languages. It may take some time to get used to them. But this is part and parcel of learning a new language. In the next post we will look at Control Flow statements.

Leave a comment