2.2 Basic Data Types (Integers, Floats, Booleans, Strings)

Introduction

Go is a statically typed language, meaning that every variable is bound to a specific data type, which must be known at compile time. Go’s basic data types include integers, floating-point numbers, booleans, and strings. Understanding these basic types is fundamental for performing various operations like arithmetic, logic, and text manipulation. This chapter covers the basic data types in Go, their characteristics, and how to use them.


1. Integer Types

Integers in Go are whole numbers, both positive and negative, without a decimal point. Go supports both signed and unsigned integers, as well as different sizes of integers to suit various use cases.

Signed Integers

Signed integers can represent both negative and positive numbers, as well as zero. Go provides several signed integer types with different ranges and storage sizes:

  • int: The default integer type, which is platform-dependent (either 32 or 64 bits).
  • int8: 8-bit signed integer (range: -128 to 127).
  • int16: 16-bit signed integer (range: -32,768 to 32,767).
  • int32: 32-bit signed integer (range: -2,147,483,648 to 2,147,483,647).
  • int64: 64-bit signed integer (range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807).

Unsigned Integers

Unsigned integers can only represent positive numbers and zero. Go provides the following unsigned integer types:

  • uint: The default unsigned integer type (32 or 64 bits, platform-dependent).
  • uint8: 8-bit unsigned integer (range: 0 to 255).
  • uint16: 16-bit unsigned integer (range: 0 to 65,535).
  • uint32: 32-bit unsigned integer (range: 0 to 4,294,967,295).
  • uint64: 64-bit unsigned integer (range: 0 to 18,446,744,073,709,551,615).

Example of Integer Declarations

package main

import "fmt"

func main() {
    var x int = 42         // Default signed integer
    var y int8 = -100      // Signed 8-bit integer
    var z uint16 = 65535   // Unsigned 16-bit integer

    fmt.Println(x, y, z)
}

Output:

42 -100 65535

Operations on Integers

You can perform common arithmetic operations on integers, such as addition, subtraction, multiplication, and division. Here's an example:

var a int = 10
var b int = 3
fmt.Println(a + b)  // Output: 13
fmt.Println(a - b)  // Output: 7
fmt.Println(a * b)  // Output: 30
fmt.Println(a / b)  // Output: 3 (integer division)

2. Floating-Point Types

Floating-point numbers in Go are used to represent numbers with decimal points. Go provides two floating-point types:

  • float32: 32-bit floating-point number.
  • float64: 64-bit floating-point number (this is the default type for float literals).

Floating-point numbers are used when you need to represent real numbers or perform precise arithmetic with decimals.

Example of Floating-Point Declarations

package main

import "fmt"

func main() {
    var pi float32 = 3.14     // 32-bit floating-point
    var e float64 = 2.71828   // 64-bit floating-point

    fmt.Println(pi, e)
}

Output:

3.14 2.71828

Operations on Floating-Point Numbers

You can perform standard arithmetic operations on floating-point numbers just like with integers:

var x float64 = 5.5
var y float64 = 2.0

fmt.Println(x + y)  // Output: 7.5
fmt.Println(x - y)  // Output: 3.5
fmt.Println(x * y)  // Output: 11.0
fmt.Println(x / y)  // Output: 2.75

Precision in Floating-Point Operations

Since float32 has less precision than float64, it’s recommended to use float64 for most floating-point calculations, especially when you need high precision.


3. Boolean Type

The bool type in Go represents a boolean value, which can either be true or false. Booleans are often used in conditional statements and logical operations.

Boolean Declaration

package main

import "fmt"

func main() {
    var isAvailable bool = true
    var isValid bool = false

    fmt.Println(isAvailable, isValid)
}

Output:

true false

Operations on Booleans

Booleans can be used in logical operations, including:

  • &&: Logical AND
  • ||: Logical OR
  • !: Logical NOT

Example of Boolean Operations

var a bool = true
var b bool = false

fmt.Println(a && b)  // Output: false (both must be true)
fmt.Println(a || b)  // Output: true  (either can be true)
fmt.Println(!a)      // Output: false (negation of a)

4. String Type

The string type in Go is used to represent a sequence of characters. Strings are immutable, meaning once a string is created, it cannot be modified. Go strings are UTF-8 encoded, so they can represent a wide range of characters, including international and special characters.

String Declaration

package main

import "fmt"

func main() {
    var greeting string = "Hello, World!"
    fmt.Println(greeting)
}

Output:

Hello, World!

String Operations

You can perform several operations on strings, including concatenation, slicing, and getting the length of a string.

Accessing Characters in a String:You can access individual characters (bytes) in a string using index notation.

greeting := "Hello"
fmt.Println(greeting[1])  // Output: 101 (ASCII value of 'e')

Note: Strings in Go are immutable, so you cannot modify individual characters directly.

Getting the Length of a String:The len() function returns the number of bytes in a string.

message := "Hello"
fmt.Println(len(message))  // Output: 5

Note: Since Go strings are UTF-8 encoded, multi-byte characters will affect the length reported by len().

Concatenation:Strings can be concatenated using the + operator.

firstName := "John"
lastName := "Doe"
fullName := firstName + " " + lastName
fmt.Println(fullName)

Output:

John Doe

Type Conversions

Go is strict about type conversions and does not automatically convert types (like integers to floats). If you want to convert a value from one type to another, you must use explicit type conversion.

Example of Type Conversion:

var x int = 10
var y float64 = float64(x)  // Convert int to float64

fmt.Println(y)  // Output: 10.0

Converting Between Strings and Other Types:

To convert basic types to strings and vice versa, you can use functions from the strconv package.

Convert a string to an integer:

var str string = "42"
var num, err = strconv.Atoi(str)
fmt.Println(num)  // Output: 42

Convert an integer to a string:

import "strconv"

var num int = 42
var str string = strconv.Itoa(num)
fmt.Println(str)  // Output: "42"

Conclusion

In this chapter, we explored Go’s basic data types: integers, floating-point numbers, booleans, and strings. These types form the foundation of most Go programs, and understanding how to declare and work with them is crucial for effective Go development. We also touched on type conversion, which is important when working with mixed data types.

In the next chapter, we will dive into control flow, covering conditionals and loops, which will allow us to make more complex and dynamic programs.

Subscribe to MicroGoLang

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe