Solution Review: Magnify a Slice

This lesson discusses the solution to the challenge given in the previous lesson.

Press + to interact
package main
import "fmt"
var s []int
func 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 magnify
fmt.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)*factor
copy(ns, s) // copying contents from s to new slice
return ns
}

Get hands-on with 1400+ tech skills courses.