import "sort" func main() { s := []string{"banana", "apple", "cherry"} sort.Strings(s) fmt.Println(s) } // Output: [apple banana cherry]
import "sort" func main() { s := []string{"apple", "banana", "cherry"} sort.Slice(s, func(i, j int) bool { return len(s[i]) < len(s[j]) }) fmt.Println(s) } // Output: [apple banana cherry]In these examples, we import the "sort" package and use its functions to sort the StringSlice. The first example sorts the slice alphabetically using the "sort.Strings" function, which takes the slice as its argument and modifies it in-place. The second example uses the "sort.Slice" function instead, which allows us to specify a custom sorting function as its second argument. In this case, we sort the slice by the length of its elements using a simple anonymous function. In conclusion, the go sort package library provides easy-to-use and powerful functions for sorting slices and arrays, including the StringSlice type.