Data Types in Golang7 min read


Data types in golang define the type of data a variable is going to store. In Go, the data types are divided into four categories –
- Basic – Numbers, strings, booleans
- Aggregate – Arrays, Structs
- Reference – Pointers, slices, maps, functions, and channels
- Interface
In this tutorial, we will be looking at basic and aggregate data types.
Table of Contents
1. Basic Data Types in Golang
The basic data types in go include the below data types,
- Numbers
- Booleans
- Strings
A. Numbers
In go, numbers are divided into 3 subcategories,
a. Integers
Integers are like whole numbers but they also include negative numbers. Both signed and unsigned integers are available in go into four different sizes as shown in below table,
int8 | 8-bit signed integer |
int16 | 16-bit signed integer |
int32 | 32-bit signed integer |
int64 | 64-bit signed integer |
uint8 | 8-bit unsigned integer |
uint16 | 16-bit unsigned integer |
uint32 | 32-bit unsigned integer |
uint64 | 64-bit unsigned integer |
int | Both in and uint contain the same size, either 32 or 64 bit. |
uint | Both in and uint contain the same size, either 32 or 64 bit. |
rune | It is a synonym of int32 and also represents Unicode code points. |
byte | It is a synonym of int8. |
uintptr | It is an unsigned integer type. Its width is not defined, but it can hold all the bits of a pointer value. |
b. Floating point numbers
The floating-point numbers include the decimal numbers and in go, they are divided into below 2 categories
float32 | 32-bit IEEE 754 floating-point number |
float64 | 64-bit IEEE 754 floating-point number |
c. Complex numbers
A complex number is one that can be written as a + bi, with a and b being real values and ‘i’ being an imaginary number. In go, the complex numbers are divided into 2 parts as shown in the below table,
complex64 | Complex numbers which contain float32 as a real and imaginary component. |
complex128 | Complex numbers contain float64 as a real and imaginary component. |
The type of a number can be printed in the output of a go program with the symbol %T.
Example:
// Go program to illustrate the use of numbers package main import "fmt" func main() { x:= 12345 // integer value y:= 86.72 // float value z:= complex(4, 8) // complex value fmt.Printf("the type of x is %T and the value of x = %d", x, x) fmt.Printf("\nthe type of y is %T and the value of y = %f", y, y) fmt.Printf("\nthe type of z is %T and the value of z = ", z) fmt.Println(z) }
Output:
C:\Users\Hiberstack\GoPrograms>go run Numbers.go the type of x is int and the value of x = 12345 the type of y is float64 and the value of y = 86.720000 the type of z is complex128 and the value of z = (4+8i)


B. Booleans
Only one bit of information, true or false, is represented by the boolean data type. The values of type boolean are not transformed to any other type, either automatically or explicitly.
Example:
// Go program to illustrate // the use of booleans package main import "fmt" func main() { str1 := "abc" str2 := "ABC" str3 := "abc" result1:= str1 == str2 result2:= str1 == str3 // Display the result fmt.Println(result1) fmt.Println(result2) // Display the type of // result1 and result2 fmt.Printf("The type of result1 is %T and " + "the type of result2 is %T", result1, result2) }
Output:
C:\Users\Hiberstack\GoPrograms>go run BooleanDemo.go false true The type of result1 is bool and the type of result2 is bool
C. Strings
A string data type is a sequence of characters. Unlike JavaScript, strings are defined between double quotes “…” rather than single quotes. By A string data type is a sequence of characters. Unlike JavaScript, strings are defined between double quotes “…” rather than single quotes. Strings are immutable, i.e. once you create a string it cannot be changed again. By default, strings in Go are encoded in UTF-8. In most circumstances, you won’t have to bother about encoding because UTF-8 supports the ASCII character set.
Let us look at an example of how to define a string in go.
Example:
//Program to eillustrate use of String in go package main import "fmt" func main(){ var s string s = "Hellow World" fmt.Println(s) }
Output:
C:\Users\Hiberstack\GoPrograms>go run StringDemo.go Hellow World
2. Aggregate Data Types in Golang
Aggregate data types in go are divided into 2 subcategories as below,
- Arrays
- Structs
A. Arrays
An array is nothing but a collection of similar types of elements. Here, the similar type of elements refers to the values having similar data types. For example, we can use Arrays to store the marks of a student. Marks are the float values. Arrays have a fixed length and therefore you need to define their length at the time of array declaration.
You can store 0 or n number of elements in an array. The array’s items are indexed using the [] index operator and their zero-based position, thus the first element’s index is array[0] and the last element’s index is array[len(array)-1].
Declaring an array –
In go, we can declare arrays in 2 ways,
1. Using var keyword
var array_name[length]datatype
Or
var array_name[length]datatype{item1, item2,…,itemN}
Example:
// Go program to illustrate how to // create an array using the var keyword // and accessing the elements of the // array using their index value package main import "fmt" func main() { // Creating an array of string type // Using var keyword var myarr[3]string // Elements are assigned using index myarr[0] = "Hiberstack" myarr[1] = "Go" myarr[2] = "lang" // Accessing the elements of the array // Using index value fmt.Println("Elements of Array:") fmt.Println("Element 1: ", myarr[0]) fmt.Println("Element 2: ", myarr[1]) fmt.Println("Element 3: ", myarr[2]) }
Output:
C:\Users\Hiberstack\GoPrograms>go run ArrayDemo1.go Elements of Array - Element 1: Hiberstack Element 2: Go Element 3: lang
2. Using shorthand
array_name:= [length]Type{item1, item2, item3,...itemN}
Example:
// Go program to illustrate how to create // an array using shorthand declaration // and accessing the elements of the // array using for loop package main import "fmt" func main() { //Shorthand declaration of array arr:= [4]string{"Hiberstack", "go", "lang", "tutorial"} // Accessing the elements of // the array Using for loop fmt.Println("Elements of the array -") for i:= 0; i < 3; i++{ fmt.Println(arr[i]) } }
Output:
C:\Users\Hiberstack\GoPrograms>go run ArrayDemo2.go Elements of the array - Hiberstack go lang
B. Structs
A Struct or Structure in golang is a user-defined data type that allows a user to group elements of different data types together. For example, we can create a structure for a person as a person has different attributes such as name, address, contact, age, etc.
Syntax:
type Person struct { name string address string contact int age int }
The type keyword creates a new type in the scenario above. It’s followed by the type name (Person) and the keyword struct to show that we’re creating a struct. Within the curly braces of the struct is a list of several fields. A name and a type are assigned to each field.
Declaring a struct –
Syntax: var a Person
The above will declare a struct by the name ‘a’ that has a type Person.
Example:
//Go program to illustrate use of Struct package main import "fmt" type Person struct { name string address string contact, age int //declaring multiple variables at a time } func main() { person1:= Person{"John", "Canada", 2379283, 28} person2:= Person{name: "Sean", address: "USA", contact: 12345678, age: 50} fmt.Println("Person1 -\n", person1) //printing all the values of person1 fmt.Println("Name of Person2 - ", person2.name) fmt.Println("Age of Person2 - ", person2.age) }
Output:
C:\Users\Hiberstack\GoPrograms>go run StructDemo.go Person1 - {John Canada 2379283 28} Name of Person2 - Sean Age of Person2 - 50