package main import ( "fmt" "sort"//第一步 ) type User struct { Name string Age int } //第二步 type Us []User //第三步 func(u Us) Len() int{ return len(u) } //第四步 func (u Us) Less(i,j int) bool{ return u[i].Age<u[j].Age } //第五步 func (m Us) Swap(i,j int){ m[i],m[j]=m[j],m[i] } func main() { users:=Us{ {"amy",20},{"jack",30},{"bob",21}, } sort.Sort(users) fmt.Println(users) }