How to create an empty Slice in Golang?
To declare the type for a variable that holds a slice, use an empty pair of square brackets, followed by the type of elements the slice will hold.
Example
package main
import (
"fmt"
"reflect"
)
func main() {
var intSlice []int
var strSlice []string
fmt.Println(reflect.ValueOf(intSlice).Kind())
fmt.Println(reflect.ValueOf(strSlice).Kind())
}
Output
slice
slice
Most Helpful This Week
How to access slice items in Golang?
How to Iterate Over a Slice in Golang?
How to Convert string to integer type in Go?
Goroutines Channels order of execution
Golang write CSV records
Go program to find MX records record of a domain
How to set timeout for http.Get() requests in Golang?
How do you handle HTTP client server security in Go?
Program in Go language to Find Largest Element of an Array
How to remove all line breaks from a string in Golang?