Data types define the type of data that a variable can hold. Golang is a statically typed language meaning that a variable needs to define the type of value it will store and once defined, the variable can not store value of any other type.
Golang support many different data types, all of which are explained in this section.
bool data type in Golang represents a boolean value. A boolean type can have only 2 values:
true
or false
. A variable of boolean type is declared using bool
keyword. Example,
package main import "fmt" func main() { var flag bool fmt.Printf("Type of variable is %T", flag) }
Above program will print
Type of variable is bool
You can also explicitly assign true or false to a bool variable as var flag bool = true
. Default value of a bool variable is false
, meaning, if you don’t assign a value to variable, it will be initialized to false
.
A string is a sequence of one or more characters enclosed between double quotes. A string data type is declared using keyword string. Example,
var string s = "codippa"
Remember that you need to use double quotes to define a string in Golang, you cannot use both single or double quotes as in some programming languages such as python or javascript.
Numeric types
All numbers fall under the category of numeric types but there are sub-types of this category which are classified according to the type of numbers. They are
Integer
This data type is used to represent whole numbers or values without decimal or fraction. Examples of this data type are summarized in the table below.
Keyword | Description | Example |
---|---|---|
uint8 |
Used to store positive integers from 0 to 255. Unsigned means only positive integers. |
var num uint8 = 10 |
uint16 |
Used to store integers from 0 to 65535. Unsigned means only positive integers. |
var num uint16 = 10000 |
uint32 |
Used to store integers from 0 to 4294967295, which is 2^32. Unsigned means only positive integers. |
var num uint32 = 3000000000 |
uint64 |
Used to store integers from 0 to 18446744073709551615, which is 2^64. Unsigned means only positive integers. |
var num uint64 = 50000000000 |
int8 |
Signed equivalent of uint8. Can store both negative and positive values in range -128 to 127. |
var num int8 = -105 |
int16 |
Same as above with range between -32768 and 32767 | var num int16 = 25000 |
int32 |
Same as above with range between -2147483648 and 2147483647 | var num unint32 = 1232312323 |
int64 |
Same as above with range between -9223372036854775808 and 9223372036854775807 | var num uint64 = 233243434342323 |
Floating point numbers
Used for decimal values such as 12.56, 99.98723 etc. There are only two variants of this data type depending on the system architecture as
Keyword | Description | Example |
---|---|---|
float32 |
Used to store floating point or decimal values. Range of this type is -3.4E+38 to 3.4E+38. |
var float32 num = 1000000000.000 |
float64 |
This data type is same as float32 but with a much larger range of -1.7E+308 to 1.7E+308. | var float64 num = 2222222222222222.88 |
complex32 |
Complex numbers with real and imaginary parts whose range is the same as that of float32. | var num complex32 = 2 + 3i |
complex64 |
Same as complex32 but with a higher range of float64. | var num complex64 = 2.232 + 5.54i |
Other numeric types
There are other data types in Golang which are variants of the types that have been already discussed above. They are.
Keyword | Description | Example |
---|---|---|
byte |
Same as uint8. If you declare a variable of this type and print its type, it will be uint8. | var byte = 125 |
rune |
Same as uint32. | var rune = 123432565 |
uint |
Either 32 or 64 bit integer(that is, same as uint32 or uint64) depending on the system architecture. | var uint = 123432565 |
int |
Same as uint | var int = 123432565 |
Note that even though uint
and uint32
or uint64
may be same on a machine but you cannot assign a variable of type uint
to a variable of type uint32
or uint64
and vice-versa. Doing so will raise an error. Example,
var x int = 125 var y uint = x
Above code sample will result in an error.
cannot use x (type int) as type uint in assignment
An explicit cast is required to use uint
, int
, uint32
and uint64
interchangeably.
Other Types
Other data types include
Arrays
Structures
Functions
Pointers
Interfaces
Maps
Channels
You will learn about them in great detail in later sections.