Menu Close

Go – Data type in go golang

In this article, We are going to learn about data type in go golang. All the variables in go golang has an associate type.
Data type defines how the data is stored in memory, what is the possible value for that particular data type and what kind of operations we can perform in go golang.

While using any built-in data type, there is no need to import any package.

Data types are divided into 4 main categories which are as follows:

  • Basic data type
  • Aggregate data type
  • Reference type 
  • Interface type

Basic data type

Basic data types are divided into 3 sub-categories which are as follows:

  • Number
  • string
  • boolean 

Numbers:

Numeric data types are used to represent numbers. Further, Numbers are divided into 3 sub-categories which are as follows:

  • integer numeric data type
  • floating-point numeric data type
  • complex numeric data type

1:) integer numeric data type:

There are 11 built-in integer numeric types.

Data TypeSizeRange
int8 Signed 8-bit integers  -128 to 127

uint8

Unsigned 8-bit integers 

0 to 255

int16

Signed 16-bit integers

-32768 to 3276

uint16

Unsigned 16-bit integers

0 to 65535

int32

Signed 32-bit integers

-2147483648 to 2147483647

uint32

Unsigned 32-bit integers

0 to 4294967295

int64

Signed 64-bit integers

-9223372036854775808 to 9223372036854775807

uint64

Unsigned 64-bit integers

0 to 18446744073709551615

int

32 / 64 bits (based on architectures )

uint

32 / 64 bits (based on architectures )

uintptr numeric data type:  It is an unsigned integer type. Its width is not defined, but its can hold all the bits of a pointer value. 

Go also support two built-in data type aliases.

  • byte data type is a alias of uint8.
  • rune is a built-in alias of int32

Example with code:

// Go program to explain the use of integers

package main

import "fmt"

func main() { 

    // Using 8-bit unsigned int
    
    var x uint8 = 225

    fmt.Println(x+1, x)

    // Using 16-bit signed int

    var y uint16 = 220

    fmt.Println(y+2, y-2)
}

Output:

226 225
222 218 

2:) floating-point numeric data type:

floating-point number or a float is used to represent real numbers that cannot be expressed as integers.

There are two built-in floating-point numeric types.

  • float32 : 32-bit floating-point numbers
  • float64 : 64-bit floating-point numbers

Example with code:

// Go program to explain the use of floating-point numbers

package main

import "fmt"

func main() {

    x := 10.12

    y := 22.12

    // Subtraction of two floating-point number     

    z := x - y

    // Display the result

    fmt.Printf("Result is: %f", z)

    // Display the type of c variable

    fmt.Printf("\nThe type of z is : %T", z)
}

Output:

Result is: -12.000000
The type of z is : float64

3:) complex numeric data type:

There are two built-in complex numeric types.

  • complex64 – Complex numbers with float32 real and imaginary parts
  • complex128 – Complex numbers with float64 real and imaginary parts

Operations on complex numbers:

You can perform arithmetic operations like addition, subtraction, multiplication, and division on complex numbers.

Example:

var x = 5 + 7i  // Type inferred as `complex128`

Go provides a built-in function named complex for creating complex numbers.

Example of complex number with code:

// Go program to explain the use of complex numbers

package main 

import "fmt"

func main() {

    var x complex128 = complex(5, 3)

    
    var y complex64 = complex(11, 4)

    fmt.Println(x)

    // Display the type

    fmt.Printf("The type of x is %T and "+ "the type of y is %T", x, y)

}

Output:

(5+3i) 
The type of x is complex128 and the type of y is complex64

Note: Both real and imaginary parts of the complex number must be of the same floating point type.

string data type:

A string is a sequence of one or more characters (letters, numbers, symbols) that can be either a constant or a variable.

You can assign string to variable by back quote (`) or double quote(“”) in go golang.  If you use the back quotes, you are creating a raw string literal. If you use the double quotes, you are creating an interpreted string literal.

Example of string data type using code:

// Go program to explain the use of strings

package main 

import "fmt"

func main() { 

    // Raw String Literals

    x := `Say "hello" to Golang!`

    fmt.Println(x)

    x = `Say "hello" to Golang!\n`

    fmt.Println(x)

    x = `This string is on
         multiple lines
         within a single back         
         quote on either side.`

    fmt.Println(x)

    // Interpreted String Literals

    x = "Say \"hello\" to Golang!"

    fmt.Println(x)
    
} 

Output:

Say "hello" to Golang!
Say "hello" to Golang!\n
This string is on          
    multiple lines          
    within a single back                   
    quote on either side. 
Say "hello" to Golang!

boolean data type:

The boolean data type can be one of two values either true or false and is defined as bool when declaring it as a data type. We can not convert values of type boolean to any other type implicitly or explicitly.

Syntax:

var var_name bool

Example of bool data type with code:

// Go program to explain the use of boolean data type

package main

import "fmt"

func main() {

    str1 := "Techieindoor" 

    str2:= "Techieindoor"

    str3:= "TechieInDoor"

    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:

true 
false 
The type of result1 is bool and the type of result2 is bool

Aggregate data type

Array types and structure types are collectively referred to as aggregate types

To learn about structure , Please refer this link:
Structure data type

Reference data type

Pointers, slices, maps, functions and channels come under this category.

Interface type

Type Conversion:

Golang has strong type system. You can not mix numeric types in an expression. For example, You cannot add an int variable to a float64 variable or even an int variable to an int64 variable. You cannot even perform an assignment between mixed types.

Example:

 
 var x int64 = 10

 var y int = x  // Compiler Error (Cannot use x (type in64) as type int in assignment)
 
 var z int = 200
 
 var result = x + z // Compiler Error (Invalid Operation: mismatched types int64 and int) 
References:

https://www.tutorialspoint.com/go/go_data_types.htm
https://www.geeksforgeeks.org/data-types-in-go/
https://www.digitalocean.com/community/tutorials/understanding-data-types-in-go

To learn more about golang, Please follow given below link:
https://www.techieindoor.com/go-lang-tutorial/
Posted in golang

Leave a Reply

Your email address will not be published. Required fields are marked *