Golang Integer Types Explained [In-Depth Tutorial]


GO

Introduction to Golang Integer Types

In Golang, there are several integer data types that cater to different needs based on the size of the integer values and whether they are signed or unsigned. Here's a list of the most common golang integer types:

  1. int: A signed integer with a platform-dependent size (usually 32 bits on 32-bit systems and 64 bits on 64-bit systems). It is designed to be the most efficient integer type for the target architecture.
  2. int8: A signed 8-bit integer with a range of -128 to 127.
  3. int16: A signed 16-bit integer with a range of -32,768 to 32,767.
  4. int32: A signed 32-bit integer with a range of -2,147,483,648 to 2,147,483,647. It is also represented by the rune type, which is an alias for int32 and is used for Unicode code points.
  5. int64: A signed 64-bit integer with a range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  6. uint: An unsigned integer with a platform-dependent size (usually 32 bits on 32-bit systems and 64 bits on 64-bit systems). It is designed to be the most efficient unsigned integer type for the target architecture.
  7. uint8: An unsigned 8-bit integer with a range of 0 to 255. It is also represented by the byte type, which is an alias for uint8.
  8. uint16: An unsigned 16-bit integer with a range of 0 to 65,535.
  9. uint32: An unsigned 32-bit integer with a range of 0 to 4,294,967,295.
  10. uint64: An unsigned 64-bit integer with a range of 0 to 18,446,744,073,709,551,615.

These different integer data types in Golang allow you to optimize your code by choosing the most suitable type based on the range and sign requirements, which can result in better memory usage and performance.

 

Different INT Data Types in GO

Below is a detailed explanation of each integer data type in Golang, highlighting their differences in terms of size, range, and usage:

 

int:

  • Signed integer
  • Platform-dependent size (usually 32 bits on 32-bit systems and 64 bits on 64-bit systems)
  • Most efficient integer type for the target architecture
  • Range depends on the platform (either the same as int32 or int64)
  • Best suited for general-purpose integer arithmetic without specific size requirements

Example:

package main

import "fmt"

func main() {
    var num int = 42
    fmt.Println("Value of num:", num)
}

In this example, we declare a variable num of type int and assign the value 42. Since int is the default integer type in Golang, it is the most common choice for general-purpose integer arithmetic.

 

int8:

  • Signed integer
  • 8-bit size
  • Range: -128 to 127
  • Useful for optimizing memory usage when working with small integer values

Example:

package main

import "fmt"

func main() {
    var num int8 = 42
    fmt.Println("Value of num:", num)
}

Here, we declare a variable num of type int8 and assign the value 42. This is useful when you need to store small integer values, optimizing memory usage.

 

int16:

  • Signed integer
  • 16-bit size
  • Range: -32,768 to 32,767
  • Suitable for memory optimization when working with integer values that fit within the 16-bit range

Example:

package main

import "fmt"

func main() {
    var num int16 = 32767
    fmt.Println("Value of num:", num)
}

In this example, we declare a variable num of type int16 and assign the maximum value of int16, which is 32767. This type is suitable for storing integer values that fit within the 16-bit range.

 

int32:

  • Signed integer
  • 32-bit size
  • Range: -2,147,483,648 to 2,147,483,647
  • Alias: rune (used for Unicode code points)
  • Appropriate for applications that require a 32-bit signed integer or Unicode code point representation

Example:

package main

import "fmt"

func main() {
    var num int32 = 2147483647
    fmt.Println("Value of num:", num)
}

Here, we declare a variable num of type int32 and assign the maximum value of int32, which is 2147483647. This type is ideal for applications that require a 32-bit signed integer representation.

 

int64:

  • Signed integer
  • 64-bit size
  • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • Ideal for working with large signed integer values or when high precision is needed

Example:

package main

import "fmt"

func main() {
    var num int64 = 9223372036854775807
    fmt.Println("Value of num:", num)
}

In this example, we declare a variable num of type int64 and assign the maximum value of int64, which is 9223372036854775807. This type is perfect for working with large signed integer values or when high precision is needed.

 

Different UINT Data Types in GO

uint:

  • Unsigned integer
  • Platform-dependent size (usually 32 bits on 32-bit systems and 64 bits on 64-bit systems)
  • Most efficient unsigned integer type for the target architecture
  • Range depends on the platform (either the same as uint32 or uint64)
  • Best suited for general-purpose unsigned integer arithmetic without specific size requirements

Example:

package main

import "fmt"

func main() {
    var num uint = 42
    fmt.Println("Value of num:", num)
}

Here, we declare a variable num of type uint and assign the value 42. This type is best suited for general-purpose unsigned integer arithmetic without specific size requirements.

 

uint8:

  • Unsigned integer
  • 8-bit size
  • Range: 0 to 255
  • Alias: byte
  • Useful for optimizing memory usage when working with small unsigned integer values or binary data

Example:

package main

import "fmt"

func main() {
    var num uint8 = 255
    fmt.Println("Value of num:", num)
}

In this example, we declare a variable num of type uint8 and assign the maximum value of uint8, which is 255. This type is useful for optimizing memory usage when working with small unsigned integer values or binary data.

 

uint16:

  • Unsigned integer
  • 16-bit size
  • Range: 0 to 65,535
  • Suitable for memory optimization when working with unsigned integer values that fit within the 16-bit range

Example:

package main

import "fmt"

func main() {
    var num uint16 = 65535
    fmt.Println("Value of num:", num)
}

Here, we declare a variable num of type uint16 and assign the maximum value of uint16, which is 65535. This type is suitable for storing unsigned integer values that fit within the 16-bit range.

 

uint32:

  • Unsigned integer
  • 32-bit size
  • Range: 0 to 4,294,967,295
  • Appropriate for applications that require a 32-bit unsigned integer representation

Example:

package main

import "fmt"

func main() {
    var num uint32 = 4294967295
    fmt.Println("Value of num:", num)
}

In this example, we declare a variable num of type uint32 and assign the maximum value of uint32, which is 4294967295. This type is appropriate for applications that require a 32-bit unsigned integer representation.

 

uint64:

  • Unsigned integer
  • 64-bit size
  • Range: 0 to 18,446,744,073,709,551,615
  • Ideal for working with large unsigned integer values or when high precision is needed

Example:

package main

import "fmt"

func main() {
    var num uint64 = 18446744073709551615
    fmt.Println("Value of num:", num)
}

Here, we declare a variable num of type uint64 and assign the maximum value of uint64, which is 18446744073709551615. This type is ideal for working with large unsigned integer values or when high precision is needed.

 

How to choose between different INT and UINT data types?

Choosing the appropriate data type in Golang largely depends on the specific requirements of your application. To make an informed decision, consider the following factors:

  1. Range: Determine the range of values your variable needs to store. For instance, if you need to store values within -128 to 127, int8 would be suitable, whereas int64 would be ideal for much larger values. For unsigned integers, consider the uint family of data types.
  2. Memory usage: Selecting a data type with the appropriate size can help optimize memory usage in your application. Smaller data types like int8 or uint8 are ideal for cases where memory conservation is important, while larger data types like int64 or uint64 are suitable when precision is critical.
  3. Signed vs. Unsigned: Determine whether your application requires signed or unsigned integers. If your variable needs to store negative values, choose a signed integer data type (int, int8, int16, int32, int64). Otherwise, consider using an unsigned integer data type (uint, uint8, uint16, uint32, uint64).
  4. Platform dependence: If you need a general-purpose integer type that offers the best performance for the target architecture, consider using int for signed integers and uint for unsigned integers. These data types have platform-dependent sizes and are optimized for the respective systems.
  5. Special use cases: For specific applications, you might need to choose data types that are designed for particular purposes. For example, when working with Unicode code points, use the rune type, which is an alias for int32. For binary data, use the byte type, an alias for uint8.

 

Summary

In summary, Golang offers a variety of integer data types to cater to different application requirements. Choosing the right data type depends on factors such as the range of values you need to store, memory usage, signed or unsigned integers, platform dependence, and any special use cases.

Golang provides signed integer types like int, int8, int16, int32, and int64, as well as unsigned integer types like uint, uint8, uint16, uint32, and uint64. Additionally, there are aliases like rune for int32 (for Unicode code points) and byte for uint8 (for binary data).

To select the most suitable data type for your application, carefully consider your specific requirements and constraints, which will help optimize memory usage, performance, and overall code efficiency. By understanding the nuances of each integer data type in Golang, you can make informed decisions that lead to better quality and more maintainable code.

 

Views: 132
Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment