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.
Add your answer
Loading...

Leave a comment

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