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 do you handle HTTP client server logging in Go?
Golang panic recover example
How do you set headers in an HTTP response in Go?
Modernizing Legacy Applications: Critical Tips for Organizational Upgrades
Golang program for implementation LZW Data Compression and Uncompression
Send and Receive values from Channel