package main

import "fmt"

// section: example
func main() {
	// Create a slice with length 2 but capacity 4
	// This means there's room to append without reallocation
	a := make([]string, 2, 4)
	a[0] = "apple"
	a[1] = "banana"

	fmt.Printf("Initial slice a: %v (len=%d, cap=%d)\n", a, len(a), cap(a))

	// Create a new slice by appending to 'a'
	// Since capacity is not exceeded, 'b' shares the same underlying array
	b := append(a, "cherry")
	fmt.Printf("After append, slice b: %v (len=%d, cap=%d)\n", b, len(b), cap(b))

	// Modifying 'b' at index 0 affects 'a' because they share the underlying array
	b[0] = "MODIFIED"

	fmt.Println("\nAfter modifying b[0]:")
	fmt.Printf("slice a: %v\n", a) // a is affected!
	fmt.Printf("slice b: %v\n", b)

	fmt.Println("\n--- Example 2: Exceeding capacity ---")

	// Now let's exceed the capacity
	c := make([]string, 2, 2)
	c[0] = "dog"
	c[1] = "cat"

	fmt.Printf("\nInitial slice c: %v (len=%d, cap=%d)\n", c, len(c), cap(c))

	// Appending here exceeds capacity, so a new array is allocated
	d := append(c, "bird")
	fmt.Printf("After append, slice d: %v (len=%d, cap=%d)\n", d, len(d), cap(d))

	// Modifying 'd' does NOT affect 'c' because they use different arrays
	d[0] = "MODIFIED"

	fmt.Println("\nAfter modifying d[0]:")
	fmt.Printf("slice c: %v\n", c) // c is NOT affected
	fmt.Printf("slice d: %v\n", d)
}

// section: example
