Solution Review: Magnify a Slice
This lesson discusses the solution to the challenge given in the previous lesson.
package mainimport "fmt"var s []intfunc main() {s = []int{1, 2, 3}fmt.Println("The length of s before enlarging is:", len(s))fmt.Println(s)s = enlarge(s, 5) // calling function to magnifyfmt.Println("The length of s after enlarging is:", len(s))fmt.Println(s)}func enlarge(s []int, factor int) []int {ns := make([]int, len(s) * factor) // making a new slice of length len(s)*factorcopy(ns, s) // copying contents from s to new slicereturn ns}
Get hands-on with 1400+ tech skills courses.