Skip to content

Latest commit

 

History

History
280 lines (207 loc) · 5.78 KB

File metadata and controls

280 lines (207 loc) · 5.78 KB

My Go Notes

Table of Contents

Introduction

  • Welcome to my Go programming notes!
  • This document serves as a comprehensive reference for various Go topics.

Syntax Basics

Variables

  • In Go, you work with variables as follows:
    • Declaring variables: var x int
    • Assigning values: x = 10
    • Short variable declaration: y := 20

Data Types

  • Go supports various data types:
    • Integer types: int, int8, int16, int32, int64
    • Unsigned integers: uint, uint8, uint16, uint32, uint64, uintptr
    • Byte type: byte (an alias for uint8)
    • Rune type: rune (an alias for int32, representing a Unicode code point)
    • Float types: float32, float64
    • String type: string
    • Boolean type: bool
    • Complex number types: complex64, complex128

Examples:

// Declaring and assigning variables
var x int
x = 10
y := 20

Control Flow

Conditional Statements

  • Use conditional statements for decision-making:
    • if, else if, else
    • switch, case, default

Examples:

// Conditional statements
if condition {
    // Code to run if the condition is true
} else if anotherCondition {
    // Code to run if the previous condition was false, and this one is true
} else {
    // Code to run if all conditions are false
}

Loops

  • Utilize loops for repetition:
    • for loops
    • range for iterating over slices and maps

Examples:

// For loop
for i := 0; i < 10; i++ {
    // Code to repeat
}

// Range for iterating over a slice
numbers := []int{1, 2, 3, 4, 5}
for _, num := range numbers {
    // Code to run for each element
}

Functions

  • Functions are essential in Go:
    • Declaring functions
    • Function parameters and return values
    • Function signatures
    • Anonymous functions (closures)
    • Recursion

Examples:

// Function with parameters and return value
func add(a, b int) int {
    return a + b
}

// Anonymous function
increment := func(x int) int {
    return x + 1
}

Packages and Modules

  • Manage code organization with packages and modules:
    • Importing packages
    • Creating and organizing your own packages
    • Working with Go modules

Examples:

// Importing packages
import "fmt"
import "math"

// Creating a custom package
// mypackage/myfile.go
package mypackage

func MyFunction() {
    // Function code
}

Error Handling

  • Handle errors effectively in Go:
    • Using the error type for error handling
    • Dealing with panics using panic and recover

Examples:

// Error handling with the `error` type
err := someFunction()
if err != nil {
    // Handle the error
}

// Panic and recover
func safeDivide(a, b int) (result int) {
    defer func() {
        if r := recover(); r != nil {
            fmt.Println("Recovered from a panic:", r)
            result = 0
        }
    }()
    result = a / b
    return result
}

Pointers

What are * and & in Go?

  • In Go, * and & are special symbols used with pointers.
    • * is like a "magic arrow" that helps you see what's inside a box (variable).
    • & is like a "magic wand" that helps you find the exact location of a box (memory address).

Examples:

// Creating Pointers with `&`
var num int = 42
var ptr *int = &num

// Dereferencing with `*`
*ptr = 100

Why Use Pointers in Go?

  • Pointers are used to:
    1. Make changes to data in one place and see those changes everywhere.
    2. Save memory when working with big data.
    3. Share data between different parts of your program.
    4. Avoid making copies of data, which can be slow and use up memory.

Examples:

// Using pointers to modify data
func modifyData(data *int) {
    *data = *data * 2
}

value := 10
modifyData(&value)

Where to Use Pointers in Go?

  • Use pointers when you want to:
    • Make changes in one place and have those changes appear everywhere.
    • Work with big data without using up too much memory.

Examples:

// Using pointers with structs
type Person struct {
    Name string
    Age  int
}

func changeName(p *Person, newName string) {
    p.Name = newName
}

p := &Person{Name: "Alice", Age: 30}
changeName(p, "Bob")

When to Use Pointers in Go?

  • Use pointers when you:
    • Need to change data inside a function and want those changes to show up outside the function.

Want to share data between different parts of your program.

  • Are dealing with big data and want to be efficient with memory.

Examples:

// Returning a pointer from a function
func createAndReturnPointer() *int {
    value := 42
    return &value
}

ptr := createAndReturnPointer()

Key Differences Between * and &

  • * is like a "magic arrow" to see what's inside a box (dereference).
  • & is like a "magic wand" to find the exact location of a box (get memory address).

Example with * and & in Go

var num int = 42
var ptr *int = &num

*ptr = 100

// Now, num is 100, and ptr points to it!

In this example, ptr is like a "magic arrow" pointing to num, and *ptr lets us change the number inside.


Useful Resources

General Notes

  • %T: Use this in fmt.Printf to print the type of a variable.
  • "Guard clause": An early return from a function when a given condition is met.

"Concurrency is not parallelism." - Rob Pike


These enhanced Go notes now include code snippets and examples to provide a more practical and hands-on approach to learning Go programming.