DebugPointer
Published on

Data types in Go Programming Language

Data types in Go Programming Language

Data Type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data. In the Go programming language(like any other language), the type of a variable determines how much space it occupies in the memory and how the bit pattern stored is interpreted.

In Go programming language, data types can be briefly classified into 4 categories-

TypeDescriptionExample
Boolean typeThey are boolean types
which can be represented
by two constants: true and false
true and false
Numeric typeThey are numbers which can
be integer or floating point
7, 3.14159
String typeA string is a set of alphanumeric
values(combination of all the characters).
Its value is a sequence of bytes.
In Go lang, Strings are immutable types
that is once created, it is not possible
to change the contents of a string.
"satvik", "bozzmob", "12.12"
Derived typeThey include - Pointer types, Array types,
Structure types, Union types, Function types,
Slice types, Interface types, Map types,
Channel Types
var a [5]int

Integer Data Types

var numberten = 10
fmt.Println(numberten)
Output
10

Operations on integers can be performed as follows-

var sum = 20 + 30
fmt.Println(sum)
Output
50

Here is a table of pre-defined integer data types

Data TypeDescriptionRange
uint8Unsigned 8-bit integers0 to 255
uint16Unsigned 16-bit integers0 to 65535
uint32Unsigned 32-bit integers0 to 4294967295
uint64Unsigned 64-bit integers0 to 18446744073709551615
int8Signed 8-bit integers-128 to 127
int16Signed 16-bit integers-32768 to 32767
int32Signed 32-bit integers-2147483648 to 2147483647
int64Signed 64-bit integers-9223372036854775808 to 9223372036854775807

Floating Point Number Data Types

var numberpi = 3.14159
fmt.Println(numberpi)
Output
3.14159

Operations on integers can be performed as follows-

var sum = 88.0 + 20.11
fmt.Println(sum)
Output
108.11

Here is a table of pre-defined integer data types

Data TypeDescription
float32IEEE-754 32-bit floating-point numbers
float64IEEE-754 64-bit floating-point numbers
complex64Complex numbers with float32 real and imaginary parts
complex128Complex numbers with float64 real and imaginary parts

Other Numeric Data Types

Data TypeDescription
bytesame as uint8
runesame as int32
uint32 or 64 bits
intsame size as uint
uintptran unsigned integer to store the uninterpreted bits of a pointer value

Booleans

The boolean data type can represent one of two predefined values, either true or false, and is defined as bool when declaring it as a data type. The value of boolean can either be assigned to a variable as a flag or can be an outcome of a logical expression(Example - 10 > 5 yields true). Booleans are used to represent the truth values that are associated with the logic branch of mathematics. Examples-

  • greater than
    • 10 > 5 true
    • 10 > 50 false
  • less than
    • 10 < 100 true
    • 100 < 10 false
  • equal
    • 10 = 10 true
    • 100 = 10 false
boolVal := 5 > 10
fmt.Println(boolVal)
Output
false

Strings

A string is a sequence of one or more characters (letters, numbers, symbols). Strings are enclosed with either back quotes ```or double quotes" in Go and have different characteristics depending on which quotes you use.

Raw String Literals

Raw string literals are a sequence of characters between back quotes `` . Any character between the quotes will appear as-is.

a := `Welcome to "DebugPointer"!\n`
fmt.Println(a)
Output
Welcome to "DebugPointer"!\n

Interpreted String Literals

Interpreted string literals are a sequence of characters between double quotes " ". Most of the time, you will end up using Interpreted String Literals, as it evaluates what's inside the double quotes.

a := "Welcome to \"DebugPointer\"!"
fmt.Println(a)
Output
Welcome to "DebugPointer"!

Arrays

An array is an ordered sequence of values, it can be numbers, strings, floating point values etc., In Go, the size or the capacity of the array is defined at the array creation time. Once the size of the array is allocated, you cannot change it at a later point of time. The size of an array is static, which means that a certain memory is allocated to the array during creation time and it cannot be altered. This is rigid, isn't it? Fret not! This is why we have Slices, which is covered in the next heading. It is very flexible.

Example of an Array-

fruits := [3]string{"apple", "mango", "banana"}
fmt.Println(fruits)
Output
[apple, mango, banana]

Slices

An array is an ordered sequence of values, it can be numbers, strings, floating point values etc., with variable length. Slices can change its length by increasing or decreasing in size. In case of a slice, if you want to add more items to it, and let's say the slice doesn't have enough memory, it will request more memory from the system based on what the program demands. Slices are often used when compared to Arrays for the same reason.

fruits := []string{"apple", "mango", "banana"}
fruits = append(fruits, "orange")
fmt.Println(fruits)
Output
[apple, mango, banana, orange]

Maps

Hash or a Dictionary in Go is called as a Map. Maps use keys and values as a pair to store data. This is useful in programming to quickly look up values by an index, or in this case, a key. You can retrieve specific value of a given key by - mapName[key]

person := map[string]string{"name": "Sam", "skills": "JavaScript", "location": "USA"}
fmt.Println(person)
fmt.Println(sammy["skills"])
Output
[name:Sam skills:JavaScript location:USA]
JavaScript