# Go Programming Course
# Go Programming Course
## Table of Contents
1. Collection Types
- Arrays
- Slices
- Maps
---
# Collection Types in Go
## Arrays
Arrays in Go provide fixed-size, ordered collections of elements. Understanding arrays is
fundamental to working with more complex data structures.
// Array length
length := len(cities) // 4
```
## Slices
Slices provide dynamic, flexible views into arrays. They are the most commonly used
collection type in Go.
// Using make
dynamicSlice := make([]int, 3, 5) // Length 3, Capacity 5
// Appending elements
numbers = append(numbers, 6, 7)
```
## Maps
Maps implement key-value pair storage, providing fast lookups and flexible data
organization.
// Checking existence
value, exists := ages["David"]
if !exists {
fmt.Println("David not found")
}
```
---
```go
type Person struct {
Name string
Age int
Address Address
}
// Creating instances
person := Person{
Name: "Alice Johnson",
Age: 30,
Address: Address{
Street: "123 Go Lane",
City: "Techville",
Country: "Gopher Land",
},
}
```
// Pointer receiver
func (p *Person) Birthday() {
p.Age++
}
```
## Struct Composition
Go uses composition over inheritance, allowing for flexible type creation.
```go
type Employee struct {
Person // Embedded struct
Position string
Salary float64
}
---
## Interface Basics
Interfaces define behavior through method signatures, enabling polymorphic code design.
```go
func processValue(v interface{}) {
switch val := v.(type) {
case int:
fmt.Printf("Integer: %d\n", val)
case string:
fmt.Printf("String: %s\n", val)
default:
fmt.Printf("Unknown type\n")
}
}
```
## Best Practices
## Practice Exercises
---