To sort a slice of strings in alphabetical order in Golang, you can use the sort.Strings()
function from the sort
package. Here's an example:
package main
import (
"fmt"
"sort"
)
func main() {
// Create a slice of strings
fruits := []string{"apple", "banana", "orange", "grape"}
// Sort the slice in alphabetical order
sort.Strings(fruits)
// Print the sorted slice
fmt.Println(fruits)
}
Output:
[apple banana grape orange]
The sort.Strings()
function modifies the order of the original slice, sorting it in ascending alphabetical order.