Go Data Types

Go Data Types

Kishore V


Go Data Types

Understanding data types is fundamental to writing efficient and reliable programs in Go. A data type defines the kind of value a variable can store, as well as how much memory it occupies.

What Are Data Types?

A data type determines:

  • The type of data (e.g., number, text, true/false)
  • The memory size allocated
  • The operations that can be performed on the value

Go is a statically typed language, meaning once a variable is declared with a specific type, it cannot store values of a different type later.

Basic Data Types in Go

Go provides three primary categories of basic data types:

1. Boolean (bool)

Represents logical values:

  • true
  • false

2. Numeric Types

Used for numbers and include:

  • Integersint, int8, int16, int32, int64
  • Unsigned integersuint, uint8, uint16, etc.
  • Floating-point numbersfloat32, float64
  • Complex numberscomplex64, complex128

3. String (string)

Represents a sequence of characters (text), enclosed in double quotes.

Example: Using Different Data Types

Available: true Quantity: 12 Price: 49.99 Product: Wireless Mouse

Type Inference in Go

Go can automatically determine the data type of a variable based on the assigned value. This is known as type inference.

int float64 string

Why Data Types Matter

Using the correct data type helps:

  • Improve performance and memory efficiency
  • Prevent type-related errors
  • Make code more readable and maintainable
  • Enable better compile-time checks

Boolean Data Type in Go

In Go, the boolean data type is used to represent logical values. A boolean variable can hold only one of two possible values:

  • true
  • false

Booleans are commonly used in conditional statements, loops, and comparisons to control the flow of a program.

Declaring Boolean Variables

A boolean variable is declared using the bool type. Go also supports type inference, allowing you to omit the explicit type in many cases.

Active: true Verified: false Completed: false Available: true

Default Value of Boolean

If a boolean variable is declared without an initial value, Go automatically assigns it a default value: false. This ensures predictable behavior and avoids uninitialized variables.

Using Booleans in Expressions

Booleans are often the result of comparisons or logical operations.

Is adult: true Has permission: false

Go Integer Data Types

In Go, integer data types are used to store whole numbers—values without decimal points—such as 42, -100, or 900000. Go provides a rich set of integer types to help you optimize memory usage and handle different numeric ranges efficiently.

Types of Integers in Go

Integer types in Go are divided into two categories:

  • 1. Signed Integers: Can store both positive and negative values. Declared using the int family of types.
  • 2. Unsigned Integers: Can store only non-negative values (zero and positive numbers). Declared using the uint family of types.

Tip: If you don’t explicitly specify a type, Go assigns the default integer type int.

Signed Integer Types

Signed integers allow both positive and negative numbers.

Temperature: -12 (Type: int) Score: 98 (Type: int)

Signed Integer Sizes and Ranges

Type Size Range
int Platform-dependent Varies (32-bit or 64-bit)
int8 8 bits -128 to 127
int16 16 bits -32,768 to 32,767
int32 32 bits -2,147,483,648 to 2,147,483,647
int64 64 bits -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Unsigned Integer Types

Unsigned integers store only non-negative values, making them ideal for quantities that cannot be negative (e.g., counts, sizes).

Items: 150 (Type: uint) Max Users: 5000 (Type: uint16)

Unsigned Integer Sizes and Ranges

Type Size Range
uint Platform-dependent Varies (32-bit or 64-bit)
uint8 8 bits 0 to 255
uint16 16 bits 0 to 65,535
uint32 32 bits 0 to 4,294,967,295
uint64 64 bits 0 to 18,446,744,073,709,551,615

Choosing the Right Integer Type

Selecting the appropriate integer type is important for performance and memory efficiency:

  • Use int for general-purpose operations
  • Use smaller types (int8, int16) when memory optimization matters
  • Use unsigned types when negative values are not needed
  • Use int64 or uint64 for very large numbers

Go Float Data Types

In Go, floating-point data types are used to store numbers that contain decimal points or require high precision—such as 3.14, -0.75, or 1.2e6. These types are essential for scientific calculations, financial data, and any scenario where fractional values are involved.

Types of Float Data in Go

Go provides two floating-point types:

Type Size Approximate Range
float32 32-bit ±3.4 × 10³⁸
float64 64-bit ±1.7 × 10³⁰⁸

Tip: The default floating-point type in Go is float64. If you don’t explicitly specify a type, Go will automatically assign float64.

Using float32

The float32 type uses less memory but provides lower precision compared to float64.

Temperature: 36.6 (Type: float32) Pressure: 101.325 (Type: float32)

When to use float32:

  • When memory optimization is important
  • When high precision is not critical

Using float64

The float64 type offers higher precision and a much larger range, making it the preferred choice in most applications.

Distance: 1.496e+11 (Type: float64) Pi value: 3.1415926536 (Type: float64)

Scientific Notation in Go

Go supports scientific notation using e or E:

Small: 0.0052 Large: 9.1e+06

Choosing Between float32 and float64

  • Use float64 for most applications (default choice)
  • Use float32 when memory usage is a concern (e.g., large datasets, graphics)
  • Prefer higher precision when dealing with financial or scientific calculations

Go String Data Type

In Go, the string data type is used to store text—a sequence of characters such as words, sentences, or symbols. Strings are defined using double quotes (" ") and are widely used for handling user input, messages, file data, and more.

Declaring Strings in Go

Go provides multiple ways to declare and initialize string variables, including explicit typing and type inference.

Greeting: "Welcome!" (Type: string) Empty Message: "" (Type: string) Title: "Go Programming" (Type: string)

Default Value of String

If a string variable is declared but not initialized, Go assigns it a default value: "". This represents an empty string, not nil.

String Characteristics

  • Strings are immutable (cannot be changed after creation)
  • Stored as a sequence of bytes (UTF-8 encoded)
  • Can include letters, numbers, symbols, and spaces

Working with Strings

Concatenation Example

Full Name: Arjun Kumar

Multi-line Strings (Raw Strings)

Go supports raw string literals using backticks (`), which preserve formatting.

This is a multi-line string in Go.


Our website uses cookies to enhance your experience. Learn More
Accept !