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

These are the primary data types in Go Lang.

Data Types Description
bool
  • Boolean type. e.g true or false
string
  • Sequence of characters. e.g “demo”, “sunit”, “sample string”, etc.
  • Generally made of individual bytes, each for a character (except for multibyte languages like Chinese, where a single cahracter may take more than 1 byte)
int
  • Integers, i.e. numbers without any decimal parts, including negative numbers also.
  • e.g 1, 3, -2 ,45, etc
  • Generally if you are working with integers you should just use the int type.
int8 / int16 / int32 / int64
  • Integer variants of varying sizes – 8 bit, 16 bit, 32 bit and 64 bit integers
uint
  • Unsigned Integers or Non-negative integers
uint8/ uint16/ uint32/ uint64
  • Unsigned Integers of varying sizes – 8 bit, 16 bit, 32 bit and 64 bit
float32
  • Floating point numbers, i.e. numbers with decimal. e.g 1.44, 6.2342, etc
  • float32 is a 32 bit single precision floating point number
float64
  • 64 bit double precision floating point number
byte
  • It is equivalent to uint8
rune
  • It is equivalent to int32

Top ∆

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.

2.1 Variables

  • It’s nothing but a reference to a value, that we can manipulate and change.
  • Variable names are Case-Sensitive. e.g person and Person are two different variables.

2.1.1 Simple variable declaration

Done using the var keyword
Syntax is var [variable-name] [data-type]

For e.g.

// 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 var declaration.
For e.g.

// 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
boolean false
string “” , i.e. an empty string
integers 0 (zero)

2.1.5 Short Hand Variable Initialization

Go makes our life even more easier with short hand initialization using := operator.

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
Some other examples are
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

Declaration of variables can also be done using the factored block.
Example: Following three variable declarations,
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
)

2.2 Constants

  • Declaration and Initialization is same as for Variables – just use the const keyword instead of var keyword
  • 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.

Top ∆


In the next blog of the Learn Go tutorial series, we will look at Packages and Imports in Go

Advertisements