dongsetan3216 2014-02-16 06:52
浏览 51
已采纳

在Go中,当使用多个return语句时,如何调用每个特定的语句?

I am attempting to have two variations of the sort method: one form which sorts the elements by name, and another that sorts the elements by salary. sort.Sort(people(data)) works when my less method compares whatever.salary. It also works if I change it to whatever.name. I would like to be able to specifically call both of these options in the less method as shown in the below code. My logic is using sort.Sort(people(data.name)) for name, and sort.Sort(people(data.salary)) for salary. These are not working. Can this even be done?

package main

import (
    "fmt"
    "sort"
)

type Comparable interface {
    Len()
    Less(i, j int) bool
    Swap(i, j int)
}

type person struct {
    name   string
    salary float64
}

func (a person) String() string {
    return fmt.Sprintf("%s: %g 
", a.name, a.salary)
}

type people []*person

func (a people) Len() int {
    return len(a)
}
func (a people) Less(i, j int) bool {
    return a[i].salary < a[j].salary
    return a[i].name < a[j].name
}
func (a people) Swap(i, j int) {
    a[i], a[j] = a[j], a[i]
}

func main() {

    var data = make(people, 10)

    var a, b, c, d, e, f, g, h, i, j person

    a.name, b.name, c.name, d.name, e.name, f.name,
        g.name, h.name, i.name, j.name = "Sheila Broflovski", "Ben Affleck",
        "Mr. Hankey", "Stan Marsh", "Kyle Broflovski", "Eric Cartman",
        "Kenny McCormick", "Mr. Garrison", "Matt Stone", "Trey Parker"

    a.salary, b.salary, c.salary, d.salary, e.salary, f.salary,
        g.salary, h.salary, i.salary, j.salary = 82000, 74000, 0, 400,
        2500, 1000, 4, 34000, 234000, 234000
    a.salary = 82000

    data[0] = &a
    data[1] = &b
    data[2] = &c
    data[3] = &d
    data[4] = &e
    data[5] = &f
    data[6] = &g
    data[7] = &h
    data[8] = &i
    data[9] = &j

    fmt.Println("


")
    fmt.Print(data)
    sort.Sort(people(data))        //This works even with the two return statements
    sort.Sort(people(data.name))   //This does not work. Exist, a version that does?
    sort.Sort(people(data.salary)) //This does not work. Exist, a version that does?
    fmt.Println("


")
    fmt.Print(data)
}
  • 写回答

2条回答 默认 最新

  • doujianchao7446 2014-02-16 07:06
    关注

    A common way to introduce the methods for sorting is to use a new type that describes the sorting condition. That's byName and bySalary here. Then you can sort using sort.Sort(byName(data)).

    Here's some demonstration code. Go also has really great good for constructing data-structures (used prolifically, for example, in table-driven tests), which also can help construct your people data here.

    package main
    
    import "fmt"
    import "sort"
    
    type person struct {
        Name   string
        Salary float64
    }
    
    type people []*person
    
    type byName people
    type bySalary people
    
    func (p byName) Len() int           { return len(p) }
    func (p byName) Less(i, j int) bool { return p[i].Name < p[j].Name }
    func (p byName) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
    
    func (p bySalary) Len() int           { return len(p) }
    func (p bySalary) Less(i, j int) bool { return p[i].Salary < p[j].Salary }
    func (p bySalary) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
    
    func main() {
        p := people{
            {"Sheila Broflovski", 82000},
            {"Ben Affleck", 74000},
            {"Mr. Hankey", 0},
            {"Stan Marsh", 400},
            {"Kyle Broflovski", 2500},
            {"Eric Cartman", 1000},
            {"Kenny McCormick", 4},
            {"Mr. Garrison", 34000},
            {"Matt Stone", 234000},
            {"Trey Parker", 234000},
        }
        fmt.Println("by name")
        sort.Sort(byName(p))
        for _, x := range p {
            fmt.Println(*x)
        }
        fmt.Println("by salary")
        sort.Sort(bySalary(p))
        for _, x := range p {
            fmt.Println(*x)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于超局变量获取查询的问题
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?