Start Coding

Topics

Go Data Types

Go, a statically typed language, offers a variety of data types to represent different kinds of values. Understanding these types is crucial for effective Go programming.

Basic Types

Numeric Types

Go provides several numeric types for integers and floating-point numbers:

  • Integers: int8, int16, int32, int64, uint8, uint16, uint32, uint64, int, uint, uintptr
  • Floating-point: float32, float64
  • Complex numbers: complex64, complex128

Boolean Type

The boolean type in Go is represented by bool, which can be either true or false.

String Type

Strings in Go are immutable sequences of bytes, typically representing text. They are enclosed in double quotes.

Composite Types

Arrays

Arrays in Go have a fixed size and contain elements of the same type. They are declared using square brackets.

Slices

Slices are dynamic, resizable views into arrays. They are more flexible than arrays and are commonly used in Go programs. Learn more about Go Slices.

Maps

Maps are Go's built-in associative data type, also known as hash tables or dictionaries in other languages. Explore Go Maps for detailed information.

Structs

Structs are composite types that group together variables under a single name. They are used to represent records or custom data types. Dive deeper into Go Structs.

Examples

Basic Types Example


package main

import "fmt"

func main() {
    var i int = 42
    var f float64 = 3.14
    var b bool = true
    var s string = "Hello, Go!"

    fmt.Printf("Integer: %d\nFloat: %f\nBoolean: %t\nString: %s\n", i, f, b, s)
}
    

Composite Types Example


package main

import "fmt"

func main() {
    // Array
    var arr [3]int = [3]int{1, 2, 3}

    // Slice
    slice := []int{4, 5, 6}

    // Map
    m := map[string]int{"one": 1, "two": 2}

    // Struct
    type Person struct {
        Name string
        Age  int
    }
    p := Person{Name: "Alice", Age: 30}

    fmt.Printf("Array: %v\nSlice: %v\nMap: %v\nStruct: %+v\n", arr, slice, m, p)
}
    

Type Inference

Go supports type inference, allowing you to omit the type declaration in many cases. The compiler will automatically deduce the type based on the value assigned.

Zero Values

In Go, variables declared without an explicit initial value are given their zero value:

  • 0 for numeric types
  • false for booleans
  • "" (empty string) for strings
  • nil for pointers, functions, interfaces, slices, channels, and maps

Type Conversions

Go requires explicit type conversions. There are no automatic type promotions or conversions between different types.

Best Practices

  • Choose the appropriate data type for your needs to optimize memory usage and performance.
  • Use type inference when it improves code readability, but be explicit when it adds clarity.
  • Be aware of the zero values for different types to avoid unexpected behavior.
  • Understand the differences between value types (like basic types and structs) and reference types (like slices and maps).

Understanding Go's data types is fundamental to writing efficient and correct Go programs. As you progress, explore more advanced concepts like Go Pointers and Go Interfaces to leverage the full power of Go's type system.