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