To sort a slice of integers in ascending order in Go, you can use the sort
package from the standard library.
Here's an example:
import (
"fmt"
"sort"
)
func main() {
numbers := []int{9, 5, 2, 7, 1}
// Sort the slice in ascending order
sort.Ints(numbers)
// Print the sorted slice
fmt.Println(numbers)
}
Output:
[1 2 5 7 9]
In this example, we import the fmt
and sort
packages. We define a slice of integers called numbers
with some random values. Then, we use sort.Ints(numbers)
to sort the slice in ascending order. Finally, we print the sorted slice using fmt.Println(numbers)
.