Make a copy

Report a typo

Suppose you have a program with a predefined slice of strings s that contains three random string elements.

Your task is to make a copy of the s slice within the solve function and then return it.

This problem uses the reflect package to check the newSlice variable and verify if you made a proper copy of the s slice. However, to solve this task, it is not necessary for you to know how the reflect package works! Your only objective is to make a proper copy of the s slice and then return it from the solve function.

Sample Input 1:

among us ඞ

Sample Output 1:

[among us ඞ]
Write a program in Go
package main

import (
"fmt"
"log"
"reflect"
)

func solve(s []string) []string {
// Write the code to make a copy of the 's' slice below:

return ? // Return the copy of the slice here!
}

// DO NOT delete or modify the contents of the main function!
func main() {
mySlice := []string{"", "", ""}
fmt.Scan(&mySlice[0], &mySlice[1], &mySlice[2])

newSlice := solve(mySlice)
if !reflect.DeepEqual(mySlice, newSlice) {
log.Fatal("new slice is not an exact copy")
}

mySlice[0], mySlice[1] = mySlice[1], mySlice[0]
if reflect.DeepEqual(mySlice, newSlice) {
log.Fatal("new slice is a reference to an old slice, but should be a copy")
}

fmt.Println(newSlice)
}
___

Create a free account to access the full topic