package main

import "fmt"

// section: example
func main() {
	// The copy() built-in function works with slices, not arrays

	// Example 1: Attempting to copy FROM an array (compile error)
	// Uncomment to see the error:
	// a := [2]int{1, 2}
	// s := []int{3, 4}
	// copy(s, a) // Compile error: cannot use a (type [2]int) as type []int

	// Example 2: Attempting to copy TO an array (compile error)
	// Uncomment to see the error:
	// s := []int{1, 2}
	// a := [2]int{3, 4}
	// copy(a, s) // Compile error: first argument to copy should be a slice

	// Workaround: Convert array to slice using slice notation
	fmt.Println("--- Workaround: Array to Slice Conversion ---")

	// Copy FROM an array: use array[:] to create a slice view
	sourceArray := [4]int{10, 20, 30, 40}
	destSlice := make([]int, 4)

	// Convert array to slice using [:]
	copy(destSlice, sourceArray[:])
	fmt.Printf("Source array: %v\n", sourceArray)
	fmt.Printf("Dest slice:   %v\n", destSlice)

	// Copy TO an array: use array[:] to create a slice view
	fmt.Println("\n--- Copy to an array using slice conversion ---")
	sourceSlice := []int{100, 200, 300}
	destArray := [3]int{}

	// Convert array to slice using [:] to allow copy
	copy(destArray[:], sourceSlice)
	fmt.Printf("Source slice: %v\n", sourceSlice)
	fmt.Printf("Dest array:   %v\n", destArray)

	// Important: The slice view shares the array's underlying data
	fmt.Println("\n--- Understanding array[:] creates a slice view ---")
	arr := [3]string{"a", "b", "c"}
	sliceView := arr[:]

	fmt.Printf("Original array: %v (type: %T)\n", arr, arr)
	fmt.Printf("Slice view:     %v (type: %T)\n", sliceView, sliceView)

	// Modifying through the slice view affects the array
	sliceView[0] = "MODIFIED"
	fmt.Printf("After modifying slice view:\n")
	fmt.Printf("Array:      %v\n", arr)
	fmt.Printf("Slice view: %v\n", sliceView)
}

// section: example
