How do you declare a slice in Go?
- s := []int{}
- s := make([]int)
- var s []int
- var s []int{}
In Go, you can declare a slice using the shorthand syntax s := []int{}. This creates an empty slice of integers. Alternatively, you can use the make() function to create a slice with a specified length and capacity. For example, s := make([]int, 0, 5) creates a slice with a length of 0 and a capacity of 5.
Loading...
Related Quiz
- When a pointer is created using the 'new' keyword, it is initialized to _______.
- You have multiple goroutines reading from a single channel in Go. How would you ensure that each goroutine receives the data only once?
- The _______ method in the database/sql package is used to close a database connection.
- The _______ function in Go is used to delete an entry from a map.
- Which command is used to run benchmarks in Go?