How to create Empty and Nil Slice?


The Sort function sorts the data interface in both ascending and descending order. IntSlice attaches the methods of Interface to []int, sorting in increasing order. StringSlice attaches the methods of Interface to []string, sorting in increasing order.

Below is an example to search particular string or integer in string or integer slice vice versa.

Example

package main

import (
    "fmt"
)

func main() {
    var nilSlice []string
    var emptySlice = []string{}
	
    fmt.Printf("\nNil:%v Len:%d Capacity:%d",nilSlice == nil, len(nilSlice), cap(nilSlice))    
	fmt.Printf("\nEmpty:%v Len:%d Capacity:%d",emptySlice == nil, len(emptySlice), cap(emptySlice))
}

Output

Nil:true Len:0 Capacity:0
Empty:false Len:0 Capacity:0
Most Helpful This Week