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.
Loading...
Related Quiz
- Explain the concept of capacity and length in slices in Go.
- _______ is a popular assertion library used in Go for testing.
- Echo is a high performance, extensible, and minimalistic web framework in Go, often compared to _____.
- How can you create a custom error in Go?
- The _______ type in the database/sql package is used to represent a nullable string value.