give me a sample code about order in golang
时间: 2024-04-17 12:26:28 浏览: 152
batect-sample-golang:基于Golang的Batect示例应用程序
Sure! Here's a simple example of ordering a slice of integers in Go:
```go
package main
import (
"fmt"
"sort"
)
func main() {
numbers := []int{8, 3, 5, 1, 4, 2, 7, 6}
// Sorting the slice in ascending order
sort.Ints(numbers)
fmt.Println("Ascending Order:", numbers)
// Sorting the slice in descending order
sort.Sort(sort.Reverse(sort.IntSlice(numbers)))
fmt.Println("Descending Order:", numbers)
}
```
In this code, we first define a slice of integers called `numbers`. We then use the `sort.Ints()` function to sort the slice in ascending order. The sorted slice is then printed using `fmt.Println()`. Next, we use `sort.Sort()` and `sort.Reverse()` to sort the slice in descending order, and again print the sorted slice.
阅读全文