In this blog we will look at Data types in Go and also look at various different ways to declare and initialize variables and constants in Go language.
This blog is part of the tutorial series – Learn Go, in which we will learn about Go language step by step.
Table of Contents
- 1.0 Data Types in Go
- 2.0 Variables and Constants
1.0 Data Types in Go
These are the primary data types in Go Lang.
|int8 / int16 / int32 / int64||
|uint8/ uint16/ uint32/ uint64||
2.0 Variables and Constants
I assume we all are aware of what variables and constants are in most programming languages. So let’s see how to declare variables and constants in Go.
- It’s nothing but a reference to a value, that we can manipulate and change.
- Variable names are Case-Sensitive. e.g
Personare two different variables.
2.1.1 Simple variable declaration
Done using the
var [variable-name] [data-type]
// declares a integer variable 'number' var number int // declares a boolean variable 'test' var test bool // declares a string variable 'name' var name string
2.1.2 Multiple variable declaration
We can also declare more than one variables of same type, in a single
// declare two variables 'x' and 'y', both integers var x, y int // declare variables s1, s2, s3, s4, s5 - all strings var s1, s2, s3, s4, s5 string
2.1.3 Variable Initialization
Sometimes, we may also want to initialize our variables when we declare them.
// declare a variable 'counter' and initialize it to 100 var counter int = 100 // declare variable 'x','y','z', and set x to 5, and y to 10 and z to 20 var x, y, z int = 5, 10, 20 // Another example with strings var s1, s2 string = "dummy", "testing" // Another example with floating point numbers var n1, n2 float64 = 13.45, 14.65
Go can also automatically infer the type of variables from the values (on the right hand side of = sign).
Hence we do not need to specify the data types for variable initialization. For. e.g we can rewrite the above variable declarations as below (by just omitting the data type in the declaration)
// 'counter' variable is initialized to integer and set to 100 var counter = 100 // Another example with three integer variable initialization var x, y, z = 5, 10, 20 // Another example with string variables initialization var s1, s2 = "dummy", "testing" // Another example with floating point variables initialization var n1, n2 = 13.45, 14.65
2.1.4 Default initialization of variables
What happens to the variables that are just declared but not initialized? What values do they hold?
for e.g when we declare variables as –
var x int OR
var name string
The answer to this is that they are ZERO-VALUED. It does not means that they are set to Zero. What is means is that every data type has a specific Zero-Value, and the variable is set to that value (if not initialized).
Some common Zero value for datatypes are
|Data Types||Zero Value|
|string||“” , i.e. an empty string|
2.1.5 Short Hand Variable Initialization
Go makes our life even more easier with short hand initialization using
We do not need to specify the
var keyword, if we use this operator to declare and initialize a variable.
For e.g, following three variable initializations are exactly the same, and the last one is the short hand initialization
var x int = 5 OR var x = 5 OR x := 5
num1, num2 := 15, 20 f1, f2, f3 := 14.2, 15.3, 3.14 // Initializing variables of different data types with a single short hand initialization. num, str, test := 100, "dummy", false
Note: One small restriction to the short hand variable initialization, is that it cannot be used outside of a function in Go. This will be more clear, when we take a look at functions in Go.
2.1.6 Factored Block for Variable Declaration
var x, y = "Hello", "World" var doRun = true var exp = 12.3
can be re-written as a factored block declaration (enclosing all the declaration within a paranthesis)
var ( x , y = "Hello", "World" doRun = true exp = 12.3 )
- Declaration and Initialization is same as for Variables – just use the
constkeyword instead of
- Does not support short hand initialization using
- Once declared and initialized, the value cannot be changed.
- Constants cannot be declared without a value. e.g following is invalid declaration for a constant –
const x int
Some examples of constant declaration are
const num, str, test = 100, "dummy", false // using factored block const ( x , y = "Hello", "World" doRun = true exp = 12.3 )
Note: One important thing to note about variables in Go, is that you will get a compile time error, if you declare a variable inside your function and not use it.
You will get an error like –
" declared and not used".
However for constants their is no such restrictions.
You can try this out when we get to functions, and write our first Go Program.
In the next blog of the Learn Go tutorial series, we will look at Packages and Imports in Go