You have an array of integers in Go, and you need to create a new slice containing the first five elements of the array. How would you accomplish this?

  • Iterating over the array and manually adding elements to the new slice
  • Using array slicing syntax: newSlice := array[:5]
  • Using the append() function to add the first five elements to a new slice
  • Using the copy() function to copy the first five elements into a new slice
Array slicing syntax in Go allows for quick creation of slices from arrays. It uses a colon (:) to specify the slice's bounds. array[:5] creates a slice containing elements from index 0 to index 4, effectively giving the first five elements of the array.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *