Golang Data Types

In this tutorial, you will learn about different DataTypes in the Go programming language. So far we discussed variables, each variable is associated with a data type that holds some value. Golang supports basic data types of numbers, characters, boolean, etc., and composite data types that are user-defined data types built using basic data types like int, float, etc. In this tutorial we will go through different classifications of both data types and in detail will understand each data type in Golang.

What are data types in Golang? 

In programming languages, data types are classification that specifies the type of data
contained by a variable. The data type defined for any variables tells the compiler how the variable declared by the programmer is intended to be used during the execution of the Golang program. Hence all values of variables have a static type, Go language can be referred to as a statically typed programming language.

  • A data type defines the type of data a variable stores in a memory location.
  • Golang is a Statically typed programming language that means the type of variables is known at compile time.
     

Declaration of Datatype

The data types are declared with keyword var for variables or with keyword const for constant variable declaration in single line or in separate lines. The <type> in syntax occupies basic data types in Golang. Let’s discuss basic data types in the next section. With the below example refresh how cons and var keywords are used by variables to define its type for better understanding refer (Golang Constants and Golang Variables tutorials)

Example 1: Syntax for declaring variables with data type


var <variable_name> <type> = <value> //var keyword
Or
const <variable_name> <type> = <value> //const keyword
Or
var <variable_name1>,<variable_name2> <type> //Multiple variable declaration
    Or
<variable_name1>,<variable_name2> := <value1><value2>
//short variable declaration

Classification of Data types

The data types in Golang are classified into two types:

GO: Data types
  1. Basic Types

    A Basic type is a data type that forms basic building blocks. It is a primitive data type. GO language supports basic data types of integer numbers, floating-point numbers, strings, boolean, and complex numbers.

  2. Composite Type

    A Composite data type is a data type constructed from primitive datatypes and with other composite types. It is also known as compound data type.

Basic Data types in Golang

Basic Data Types are mainly classified into

  1. Number
  2. String
  3. Boolean
GO: Data types

Let us understand each basic data type in detail

Number Datatype in Golang

A number or numeric data type represents integers, floating numbers, and complex numbers. Eg integer numbers like 34,345 etc, floating numbers with decimal points 45.89, 5.8, etc, and complex numbers of the form a+bi, 34 +78i.

Let us start with numeric data types. There are three main types of numeric types in programming:

1. Integer(int) Data type in Golang

An integer data type is a range of whole numbers representing a group of binary digits. (bits) .In Golang integers can have positive, negative, or zero values.

Basic integer data types are further classified as :

  1. Signed
  2. Unsigned
GO: Data types

“int” represents signed integers and “uint” represents unsigned integers

Signed integer data type

A signed integer data type is the default type that is machine-dependent and supports 32-bit(4 bytes) & 64-bit (8 bytes) machines. This numeric data type stores positive and negative values, they are available in 5 different sizes(types) with different ranges.

int int8 int16 int32 int64

 

The given table below represents in detail the types of signed data types, their size, and various ranges.

TYPE SIZE RANGE
int 32 bits/4 bytes or 64 bits/8 bytes  
int8 8 bits/1 byte -128 to 127
int16 16 bits/2 bytes -32768 to 32767
int32 32 bits/4 bytes -2147483648 to 214748367
int64 64 bits /8 bytes -9223372036854775808 to 9223372036854775807

 

Consider inference from the table: int8 is a signed integer with have a value from -128 to 127,int16 is a signed integer with having a value from -32768 to 32767, etc.

Let’s discuss all types of signed integer data types and how they are declared in Golang with the corresponding syntax

var a int      //declares variable a of int integer type

var a int8     //declares variable a of integer type int8

var a int16   //declares variable a of integer type int16

var a int32    //declares variable a of integer type int32

var a int64    //declares variable a of integer type int64 

Let’s understand how to use different int data types with two program examples. Declaration using int is easy & simple; many Golang programs using this data type are mentioned in earlier tutorials.

How to declare and print type of data types int8 and int 16.


package main
import (
    "fmt"
    "reflect"
    "unsafe"
)
func main() {
    //Declare a int 8
    var Type1 int8 = 6
      //Declare a int16
    var Type 2 int16 = 6
    //Size of int8 in bytes
    fmt.Printf("%d bytes\n", unsafe.Sizeof(Type1))
    fmt.Printf("Type1's type is %s\n", reflect.TypeOf(Type1))
     //Size of int16 in bytes
    fmt.Printf("%d bytes\n", unsafe.Sizeof(Type2))
    fmt.Printf("Type2's type is %s\n", reflect.TypeOf(Type2)
}

Output:


1 bytes
Type1's type is int8
2 bytes
Type2's type is int16

Program 2 How to declare and print type of datatypes int32,int64


package main
import (
    "fmt"
    "reflect"
    "unsafe"
)

func main() {
    //Declare a int32
    var Type3 int32 = 6
    
    //Declare a int64
    var Type4 int64 = 6
    
    //Size of int32 in bytes
    fmt.Printf("%d bytes\n", unsafe.Sizeof(Type3))
    fmt.Printf("Type3's type is %s\n", reflect.TypeOf(Type3))
    
    //Size of int64 in bytes
    fmt.Printf("%d bytes\n", unsafe.Sizeof(Type4))
    fmt.Printf("Type4's type is %s\n", reflect.TypeOf(Type4))
}

Output:


4 bytes
Type3's type is int32
8 bytes
Type4's type is int64

Unsigned integer data type

An unsigned integer data type (uint)  is a default type that is machine-dependent and supports 32-bit(4 bytes) & 64-bit (8 bytes) machines. This numeric data type holds only negative values, they are available in 6 different sizes(types) and various ranges.

uint uint8 uint16 uint32 uint64 uintptr

 

TYPE  SIZE RANGE
uint 32 bits/4 bytes or 64 bits/8 bytes 0 to 4294967295 Or  
uint8   8 bits/1 byte 0 to 25
uint16 16 bits/2 bytes 0 to 65535
uint32 32 bits/4 bytes 0 to 4294967295
uint64 64 bits /8 bytes 0 to 18446744073709551615
uintptr Uninterpreted bits Pointer values

The above-given table represents in detail the types of Unsigned data types, their size, and various ranges. Consider inference from the table.

  • uint8 is a signed integer with have a value from 0 to 255,uint16  is a signed integer with having a value from 0 to 65535, etc
  • uintptr is an unsigned integer data type large enough to hold the uninterpreted bits of a pointer value.

Syntax to declare the uint integer data type


var a uint      //declares variable a of unsigned integer type uint

var a uint8     //declares variable an of integer type uint8

var a uint16   //declares variable an of integer type uint16

var a uint32    //declares variable an of integer type uint32

var a int64    //declares variable an of integer type uint64 

uintptr

  • size and range are platform-dependent.
  • unsigned integer type that stores pointer address.

Let’s understand how to use different uint data types with two program examples

Program 1:How to declare and print uint type of data types with uint, int8, and int 16.


package main
import (
    "fmt"
    "reflect"
    "unsafe"
)
    func main() {
    //Declare a uint8    
      var type1 uint8 = 2
     //Size of uint8 in bytes
    fmt.Printf("%d bytes\n", unsafe.Sizeof(type1))
    fmt.Printf("type1's type is %s\n", reflect.TypeOf(type1))
    //Declare a uint16
    var type 2 uint16 = 2
    //Size of uint16 in bytes
    fmt.Printf("%d bytes\n", unsafe.Sizeof(type2))
    fmt.Printf("type2's type is %s\n", reflect.TypeOf(type2))
   }


Output:


1 bytes
Type1's type is uint8
2 bytes
Type2's type is uint16

Program 2 : How to declare and print type of data types using int32,int64 


package main
import (
   "fmt"
    "reflect"
    "Unsafe" )
func main() {
    //Declare a uint32
     var type3 uint32 = 2
      //Size of uint8 in bytes
    fmt.Printf("%d bytes\n", unsafe.Sizeof(type3))
    fmt.Printf("type3's type is %s\n", reflect.TypeOf(type3))
     //Declare a uint64
    var type4 uint64 = 2
   //Size of uint16 in bytes
    fmt.Printf("%d bytes\n", unsafe.Sizeof(type4))
    fmt.Printf("type4's type is %s\n", reflect.TypeOf(type4))
 }


Output:


4 bytes
Type3's type is uint32
8 bytes
Type4's type is uint64

2. Floating-point Datatype in Golang

A floating-point is a numeric data type that represents decimal point values like 45.6,0.00005666

In Golang floating point is classified into two types: Float32 and Float64.

Type Size Range
Float32 32 bits/4 bytes -3.4e+38 to 3.4e+38 
Float64 64 bits /8 bytes -1.7e+308 to +1.7e+308
  1. Float32 is a float data type of 32bits

    The syntax used to declare float32 data type is

    var  a float32
    
    

    Program to declare and print a float32 data type

    
    package main
    import (
        "fmt"
        "reflect"
        "unsafe"
    )
    func main() {
        //Declare a float32
        var a float32 = 2   
        //Size of float32 in bytes
        fmt.Printf("%d bytes\n", unsafe.Sizeof(a))
        fmt.Printf("a's type is %s\n", reflect.TypeOf(a)) 
    }
    
    
    

    Output:

    
    4 Bytes
    a's type is float32
    
  2. Float64 is a float data type of 64bits

    The syntax used to declare float64 data type is

    var  a float64
    
    

    Program to declare and print a float64 data type

    
    package main
     
    import (
        "fmt"
        "reflect"
        "unsafe"
    )
     
    func main() {
        //Declare a float64
        var a float64 = 2
        
        //Size of float64 in bytes
        fmt.Printf("%d bytes\n", unsafe.Sizeof(a))
        fmt.Printf("a's type is %s\n", reflect.TypeOf(a))
        
        //Default is float64 when you don't specify a type
        b := 2.3
        fmt.Printf("b's type is %s\n", reflect.TypeOf(b))
    }
    
    

    Output:

    
    8 Bytes
    a's type is float64
    b's type is float64
    

Note : short declaration of a variable with any floating-point number detects as float64 data type. Float64 is a default data type if the type is not mentioned.


3. Complex number

A complex number is of the form a+ib, a real part, and ib is an imaginary part.

Complex numbers are initialized in two ways

  1. Using a complex function both real and imaginary parts should be of the same data type either float32 or float64
    complex(a,b)
    var a int64    //declares variable a of integer type int64 
    
    
  2. Shorthand declaration using:= in case direct numbers are provided without any other initialization to a variable. In such case, the default type is complex number128
     
    A := 9 + 20i
    
    

In Golang complex numbers have two data types Complex number64 and Complex number128

Type Feature
ComplexNumber64 Float32 forms real and imaginary part
ComplexNumber128 Float64 forms real and imaginary part
  1. Complex number64

    The size of real and imaginary parts are float32 type i.e. size of 32 bits or 4 bytes.

    The range of complex number64 is the same as float32 ie 1.2E-38 to 3.4E+38

    Program to declare and print a complexnumber64 data type

    
    package main
    import (
        "fmt"
        "reflect"
        "unsafe"
    )
    func main() {
        var a float32 = 7
        var b float32 = 2
        
        //Initialize-1
        c := complex(a, b)
        
        //Initialize-2
        var d complex64
        d = 4 + 5i
        
        //Print Size
        fmt.Printf("c's size is %d bytes\n", unsafe.Sizeof(c))
        fmt.Printf("d's size is %d bytes\n", unsafe.Sizeof(d))
        
        //Print type
        fmt.Printf("c's type is %s\n", reflect.TypeOf(c))
        fmt.Printf("d's type is %s\n", reflect.TypeOf(d))
        
        //Operations on complex number
        fmt.Println(c+d, c-d, c*d, c/d)
    }
    
    
    

    The program has given the above-used initialization methods to initialize any variable as complex numbers.Two variables a and b are declared of type float 32 further assigned to variable c by complex (). So now c contains a variable of type complex number64 since float 32 types are used for declaration of variables a and b. Next, a variable d is declared as data type complex 64 and directly assigns a value in complex form using shorthand declaration ie

    d := 4 + 5i

    Output:

    
    c's size is 8 bytes
    d's size is 8 bytes
    c's type is complex64
    d's type is complex64
    (11+7i) (3-3i) (18+43i) (0.9268293-0.6585366i)
    
  2. Complex number128

    The size of real and imaginary parts are float64 type i.e. size of 64 bits or 8 bytes.

    Range of complex number128 is same as float64 ie -1.7E+308 to +1.7E+308

    Program to declare and print a complexnumber64 data type

    
    package main
    import (
        "fmt"
        "reflect"
        "unsafe"
    )
    func main() {
        var a float64 = 3
        var b float64 = 5
        
        //Initialize-1
        c := complex(a, b)
        
        //Initialize-2. When don't specify a type , the default type will be complex128
        d := 4 + 5i
        
        //Print Size
        fmt.Printf("c's size is %d bytes\n", unsafe.Sizeof(c))
        fmt.Printf("d's size is %d bytes\n", unsafe.Sizeof(d))
        
        //Print type
        fmt.Printf("c's type is %s\n", reflect.TypeOf(c))
        fmt.Printf("d's type is %s\n", reflect.TypeOf(d))
        
        //Operations on complex number
        fmt.Println(c+d, c-d, c*d, c/d)
    }
    
    
    
    

    a =3 & b=5 assigned as float64 type using var keyword.The function call complex() assigns to variable c the value 3 + 5i as a complex number.Now c = 3 + 5i. Using shorthand declaration := d is assigned 4 + 5i.The operation addition (c+d) adds the complex numbers to give output 7 + 10i.The rest of the operations given in the program also work in the same way.

    Output:

    
    c's size is 16 bytes
    d's size is 16 bytes
    c's type is complex128
    d's type is complex128
    (7+10i) (-1+0i) (-13+35i) (0.902439024390244+0.12195121951219513i)
    

The basic integer data types are also of two more types: Byte and Rune. Byte & Rune are two integer data types that alternatively refer to uint8 and int32 data types, respectively.

  1. Byte: The byte data type represents ASCII characters.

    How to declare byte?
    Var name1 byte = 'B' // byte data type 
    

    For example, a byte variable with value 'a' is converted to the integer 97, a byte variable with value 'b' is converted to the integer 98, etc.

  2. Rune: rune data type represents Unicode characters that are encoded in UTF-8 format.Eg:-In Go, Characters are expressed by enclosing them in single quotes like this: 'c'.

    How to declare rune?
    var character = 'C ‘ // Type inferred as rune which is the default type for character values var r rune = ‘~’ // rune data type 
    

    For example:  a rune variable r with a Unicode value '~' is converted to the corresponding Unicode codepoint U+007E, where U+ means Unicode and the numbers are hexadecimal, which is essentially an integer.

NOTE:

1. In Golang, there is no char data type rather it uses byte and rune to represent character values in Golang.

2. The default type for character values is the rune

String Datatype in Golang

A string is another mostly used basic data type in Golang which is a sequence of characters to form a text. a digit, a letter, symbol represented inside double quotes. Example: Consider the word Golang, in the Go language it is represented as a string by representing inside double quotes“ Golang”, a sequence of digits 6456657, symbols like +_abcr forms string inside quotes.
var text = “Golang”

var A = “6456657”

var B = “+_abcr”

Program 8: String


package main
import ("fmt")

func main() {
/* var keyword assigns variable text with value “Golang” of string type */
  var text string = "Golang"
/* short declaration to txt2 of string */
  txt2 := "12345"

  fmt.Printf("Type: %T, value: %v\n", text, text)
 fmt.Printf("Type: %T, value: %v\n", txt2, txt2)
 
}

Output:


Type : string,  value : Golang
Type : string,  value : 12345

NOTE: Anything inside quotes forms a string. For example, txt2 is assigned with numbers 12345, but its type is string not integer since it is given inside double quotes

Boolean Datatype in Golang

A boolean datatype is declared using bool keywords with two possible inferences either false or true.

  • The default type of boolean is false.
    1. AND: the output is true only if two given inputs are true else false
    2. OR: Output is true when any two input is true else false
    3. NEGATION is just the opposite of resultant output.

boolean type and its operations



package main
import "fmt"

func main() {
    //Default value will be false it not initialized
    var v bool
    fmt.Printf("a's value is %t\n", v)
    
    //And operation on one true and other false
    andOperation := 1 < 2 && 1 > 3
    fmt.Printf("Ouput of AND operation on one true and other false %t\n", andOperation)
    
    //OR operation on one true and other false
    orOperation := 1 < 2 || 1 > 3
    fmt.Printf("Ouput of OR operation on one true and other false: %t\n", orOperation)
    
    //Negation Operation on a false value
    negationOperation := !(1 > 2)
    fmt.Printf("Ouput of NEGATION operation on false value: %t\n", negationOperation)
}

Output:



v's value is false
Output of AND operation on one true and other false : false
Output of OR operation on one true and other false : true Output of NEGATION operation on false value : true 

Summary

The basic data types in Golang are :

Basic Data Type Description  Example
int Integer number 0,567,9,-8766
float Decimal point numbers 4.5,0.66,-67.98
string Sequence of characters “ Go Lang” “Learn e tutorial”
bool Either True or False true,false
Complex number a+ib form numbers 5+4i,-7+76i
byte A byte of nonnegative integers . 115,2,97
rune Used for characters ‘>’, ‘8’, ‘i’

Composite Datatypes

A composite data type is any data type that is constructed by a programmer for declaring a single variable that is capable of holding a collection of values with the help of primitive/basic data types like int, float, bool, a string of Go programming language.

  • Primitive/basic data types are the building blocks of composite data types.
  • They are also known as “unnamed types”.
  • It is known as an unnamed type because they use a literal type to define the structural representation.
  • Example Composite types: arrays, slices, maps, and structs.

Classification of composite Data type

The composite data type is mainly of three types:

  1. Non-Reference Type(Aggregation)
    • Arrays and structs are aggregate types, whose values are concatenations of other values in memory.
    • Arrays are homogeneous. The elements all have the same type.
    • Structs are heterogeneous.
    • Both arrays and structs are fixed size
  2. Reference Type
    • Slices and maps are dynamic data structures that grow as values are added.
  3. Interface
GO: Data types