Skip to content

Instantly share code, notes, and snippets.

@knbr13
Created May 5, 2024 13:26
Show Gist options
  • Save knbr13/d01441f6ea2c1af94c7e7d98ea190fae to your computer and use it in GitHub Desktop.
Save knbr13/d01441f6ea2c1af94c7e7d98ea190fae to your computer and use it in GitHub Desktop.
Data Types in Go

Data Types in Go

Go is a statically-typed language, which means that every variable has a specific data type that determines the kind of value it can hold. Go has several built-in data types that are categorized into four main groups: basic types, aggregate types, reference types, and interface types.

Basic Types

Basic types are the primitive data types in Go. They include:

  1. bool: Represents a boolean value, either true or false.

    var isTrue bool = true
  2. numeric types:

    • int (signed integers): int8, int16, int32, int64
    • uint (unsigned integers): uint8, uint16, uint32, uint64
    • float (floating-point numbers): float32, float64
    • complex (complex numbers): complex64, complex128
    • byte: An alias for uint8, used to represent raw data (e.g., encoding/decoding, file I/O).
      var data byte = 'A' // byte is an alias for uint8
  3. string: Represents a sequence of Unicode code points. Strings are immutable in Go.

    var name string = "John Doe"
  4. rune: Represents a Unicode code point. It is an alias for int32.

    var letter rune = 'A'

The byte type is commonly used when dealing with raw data, such as encoding/decoding data formats or reading/writing files. It's an alias for uint8 and can store values from 0 to 255.

Aggregate Types

Aggregate types are types that group multiple values of the same or different types into a single entity. They include:

  1. Array: A fixed-length collection of elements of the same type.

    var scores = [5]int{92, 85, 77, 88, 95}
  2. Struct: A collection of fields of potentially different types.

    type Person struct {
        Name string
        Age  int
    }
    person := Person{Name: "Alice", Age: 25}

Arrays in Go are value types, which means that when you assign an array to a new variable, a copy of the original array is created. Structs, on the other hand, are aggregate types that group different types together.

Reference Types

Reference types are types that store memory addresses or references to values. They include:

  1. Pointer: Stores the memory address of a value.

    var x int = 10
    var ptr *int = &x // ptr stores the address of x
  2. Slice: A reference to a contiguous segment of an array. Slices are lightweight and provide a more flexible way to work with collections of elements than arrays.

    numbers := []int{1, 2, 3, 4, 5}
  3. Map: An unordered collection of key-value pairs.

    ages := map[string]int{
        "Alice": 25,
        "Bob":   30,
    }
  4. Channel: A way to communicate between goroutines (concurrent execution units).

    ch := make(chan int)
    go func() { ch <- 42 }() // Send 42 to the channel
    value := <-ch // Receive a value from the channel

Slices, maps, and channels are reference types, which means that when you assign them to a new variable, only the reference is copied, not the underlying data.

Interface Type

An interface type defines a set of method signatures. Any concrete type that implements all the methods of an interface is considered to be an implementation of that interface. Interfaces are a way to achieve abstraction and polymorphism in Go.

type Shape interface {
   Area() float64
   Perimeter() float64
}

Understanding these types and their characteristics is crucial for writing efficient and readable Go code.

Notes:

  • When you declare a new variable using the var keyword without assigning it a value, it will be automatically assigned the zero value of its type. The zero value is the default value that a variable of a particular type will have when it is declared but not explicitly initialized. Here's a list of the zero values for different data types in Go:

  • bool: false

  • int (including all variants of it, e.g. int8, uint16, ...): 0

  • float (float32 and float64): 0.0

  • complex (complex64 and complex128): 0 + 0i

  • string: ""

  • Pointers: nil

  • Arrays: All elements are set to the zero value of the array's element type.

  • Slices and maps: nil

  • Channels: nil

  • Interfaces: nil

  • Struct: All fields are set to their respective zero values

Examples:

var i int           // i is equal to 0
var b byte          // b is equal to 0 
var f float64       // f is equal to 0.0
var c complex128    // c is equal to (0.0 + 0.0i)
var str string      // str is equal to ""
var r rune          // r is equal to 0
var bl bool         // bl is equal to false
var ptr *int        // ptr is equal to nil
var arr [3]int      // arr is equal to [0, 0, 0]
var slc []int       // slc is equal to nil
var ch chan int     // ch is equal to nil
var m map[string]int // m is equal to nil
var iface interface{} // iface is equal to nil
var st struct{ x, y int } // st is equal to {0, 0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment