Golang Tutorial

Fundamentals

Control Statements

Functions & Methods

Structure

Arrays & Slices

String

Pointers

Interfaces

Concurrency

Data Types in Golang

In Go, data types specify the type of data a variable can hold. Understanding these types is fundamental for any Go programmer. Let's dive into the various data types in Go:

1. Basic Types

1.1. Numeric Types

  • Integers: Go supports several types of integer values:
    • Signed integers: int8, int16, int32, int64, and int (size depends on the platform: 32 or 64 bits)
    • Unsigned integers: uint8 (also byte), uint16, uint32, uint64, and uint
    • Special types: uintptr (an integer type that can store a pointer)
var i int = 42
var u uint = 42
  • Floating-point numbers: There are two sizes of floating-point numbers:
    • float32
    • float64
var f float32 = 3.14
  • Complex numbers: There are two types of complex numbers:
    • complex64
    • complex128
var c complex64 = 1 + 2i

1.2. Boolean

The bool type represents true or false values.

var isTrue bool = true

1.3. Strings

A string represents a sequence of characters:

var str string = "Hello, Gophers!"

2. Derived Types

2.1. Pointers

Pointers hold the memory address of another variable:

var x int = 10
var p *int = &x

2.2. Arrays

Arrays are sequences of elements of the same type:

var arr [5]int = [5]int{1, 2, 3, 4, 5}

2.3. Slices

Slices are similar to arrays but with a dynamic size:

slice := []int{1, 2, 3, 4, 5}

2.4. Maps

Maps are collections of key-value pairs:

m := map[string]int{
    "apple":  5,
    "banana": 2,
}

2.5. Structs

Structs are collections of fields with different data types:

type Person struct {
    Name string
    Age  int
}

var p Person = Person{Name: "Alice", Age: 30}

2.6. Channels

Channels are used for communication between goroutines:

ch := make(chan int)

2.7. Functions

In Go, functions are first-class citizens and can be assigned to variables:

func sayHello() {
    fmt.Println("Hello!")
}

var helloFunc func() = sayHello

3. Interface Types

Interfaces define a contract (set of methods), and any type that satisfies this contract implicitly implements the interface:

type Writer interface {
    Write([]byte) (int, error)
}

Conclusion

Go has a rich set of data types that cater to different programming needs, from basic types like integers and floats to more complex types like maps and channels. Understanding and effectively using these types is essential for writing efficient and readable Go programs.

  1. Basic Data Types in Golang:

    • Go has several basic data types, including int, float64, bool, string, and complex.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          var integer int = 42
          var floatingPoint float64 = 3.14
          var boolean bool = true
          var text string = "Hello, Golang!"
          var complexNumber complex128 = 1 + 2i
      
          fmt.Println(integer, floatingPoint, boolean, text, complexNumber)
      }
      
  2. Numeric Data Types in Golang:

    • Numeric data types include integers (int, int8, int16, int32, int64), unsigned integers (uint, uint8, uint16, uint32, uint64), and floating-point numbers (float32, float64).
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          var integer int = 42
          var unsignedInt uint = 42
          var floatingPoint float64 = 3.14
      
          fmt.Println(integer, unsignedInt, floatingPoint)
      }
      
  3. String Data Type in Golang:

    • The string type represents sequences of characters.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          greeting := "Hello, Golang!"
          fmt.Println(greeting)
      }
      
  4. Composite Data Types in Golang:

    • Composite data types include arrays, slices, maps, and structs.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          // Example using arrays, slices, maps, and structs
          // (refer to examples in points 5, 6, and 9)
      }
      
  5. Arrays and Slices in Golang:

    • Arrays have a fixed size, while slices are dynamically resizable.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          // Array
          var arr [3]int = [3]int{1, 2, 3}
      
          // Slice
          slice := []int{4, 5, 6}
      
          fmt.Println(arr, slice)
      }
      
  6. Structs and Custom Data Types in Golang:

    • Structs allow you to create custom data types with named fields.
    • Example:
      package main
      
      import "fmt"
      
      // Define a struct
      type Person struct {
          Name string
          Age  int
      }
      
      func main() {
          // Create an instance of the struct
          person := Person{"Alice", 25}
          fmt.Println(person)
      }
      
  7. Pointers and Reference Types in Golang:

    • Pointers store memory addresses. Reference types include slices, maps, channels, and interfaces.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          // Pointer example
          var x int = 42
          var ptr *int = &x
      
          // Reference type (slice)
          slice := []int{1, 2, 3}
      
          fmt.Println(*ptr, slice)
      }
      
  8. Type Conversion in Golang:

    • Use explicit type conversion to convert between different data types.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          var x int = 42
          var y float64 = float64(x)
      
          fmt.Println(y)
      }
      
  9. Golang Data Types and Their Sizes:

    • The unsafe package can be used to check the size of a data type.
    • Example:
      package main
      
      import (
          "fmt"
          "unsafe"
      )
      
      func main() {
          var x int
          fmt.Println("Size of int:", unsafe.Sizeof(x))
      }