- Introduction
- Syntax Basics
- Data Types
- Control Flow
- Functions
- Packages and Modules
- Error Handling
- Pointers
- Welcome to my Go programming notes!
- This document serves as a comprehensive reference for various Go topics.
- In Go, you work with variables as follows:
- Declaring variables:
var x int - Assigning values:
x = 10 - Short variable declaration:
y := 20
- Declaring variables:
- 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 foruint8) - Rune type:
rune(an alias forint32, representing a Unicode code point) - Float types:
float32,float64 - String type:
string - Boolean type:
bool - Complex number types:
complex64,complex128
- Integer types:
// Declaring and assigning variables
var x int
x = 10
y := 20- Use conditional statements for decision-making:
if,else if,elseswitch,case,default
// 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
}- Utilize loops for repetition:
forloopsrangefor iterating over slices and maps
// 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 are essential in Go:
- Declaring functions
- Function parameters and return values
- Function signatures
- Anonymous functions (closures)
- Recursion
// 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
}- Manage code organization with packages and modules:
- Importing packages
- Creating and organizing your own packages
- Working with Go modules
// Importing packages
import "fmt"
import "math"
// Creating a custom package
// mypackage/myfile.go
package mypackage
func MyFunction() {
// Function code
}- Handle errors effectively in Go:
- Using the
errortype for error handling - Dealing with panics using
panicandrecover
- Using the
// 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
}- 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).
// Creating Pointers with `&`
var num int = 42
var ptr *int = &num
// Dereferencing with `*`
*ptr = 100- Pointers are used to:
- Make changes to data in one place and see those changes everywhere.
- Save memory when working with big data.
- Share data between different parts of your program.
- Avoid making copies of data, which can be slow and use up memory.
// Using pointers to modify data
func modifyData(data *int) {
*data = *data * 2
}
value := 10
modifyData(&value)- 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.
// 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")- 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.
// Returning a pointer from a function
func createAndReturnPointer() *int {
value := 42
return &value
}
ptr := createAndReturnPointer()*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).
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.
%T: Use this infmt.Printfto 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.