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 set cookies in an HTTP request with an HTTP client in Go?
How do you handle HTTP server caching in Go?
Golang Program to Triangle of Alphabets
Create and Print Multi Dimensional Slice in Golang
GO language program with an example of Hash Table
Unveiling the Potential of Quantum Computing in AI: A Game Changer for Industries
Naming Conventions for Golang Functions
Web Application to generate QR code in Golang
How to check specific field exist in struct?
Example to compare Println vs Printf