我只是有一个问题,我有一个结构数组,例如
package main
import "log"
type Planet struct {
Name string `json:"name"`
Aphelion float64 `json:"aphelion"` // in million km
Perihelion float64 `json:"perihelion"` // in million km
Axis int64 `json:"Axis"` // in km
Radius float64 `json:"radius"`
}
func main() {
var mars = new(Planet)
mars.Name = "Mars"
mars.Aphelion = 249.2
mars.Perihelion = 206.7
mars.Axis = 227939100
mars.Radius = 3389.5
var earth = new(Planet)
earth.Name = "Earth"
earth.Aphelion = 151.930
earth.Perihelion = 147.095
earth.Axis = 149598261
earth.Radius = 6371.0
var venus = new(Planet)
venus.Name = "Venus"
venus.Aphelion = 108.939
venus.Perihelion = 107.477
venus.Axis = 108208000
venus.Radius = 6051.8
planets := [...]Planet{*mars, *venus, *earth}
log.Println(planets)
}
假设您想按 对其进行排序Axis。你是怎样做的?
(注意:我已经看过http://golang.org/pkg/sort/并且它似乎有效,但我必须添加大约 20 行只是为了通过一个非常简单的键进行简单排序。我有一个 python 背景就像sorted(planets, key=lambda n: n.Axis)- 在 Go 中有类似的简单吗?)
精慕HU
相关分类