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

huangapple go评论85阅读模式
英文:

In Go, when using multiple return statements, how do you invoke each specific one?

问题

我正在尝试有两个不同的sort方法变体:一个按照姓名排序元素,另一个按照薪水排序元素。当我的less方法比较whatever.salary时,sort.Sort(people(data))可以工作。如果我将其更改为whatever.name,它也可以工作。我希望能够在less方法中特别调用这两个选项,如下面的代码所示。我的逻辑是使用sort.Sort(people(data.name))按姓名排序,使用sort.Sort(people(data.salary))按薪水排序。但是这些方法都不起作用。这种做法可行吗?

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 \n", 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("\n\n\n")
	fmt.Print(data)
	sort.Sort(people(data))        // 这个可以工作,即使有两个返回语句
	sort.Sort(people(data.name))   // 这个不起作用。是否存在可以工作的版本?
	sort.Sort(people(data.salary)) // 这个不起作用。是否存在可以工作的版本?
	fmt.Println("\n\n\n")
	fmt.Print(data)
}

以下是翻译好的代码:

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 \n", 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("\n\n\n")
	fmt.Print(data)
	sort.Sort(people(data))        // 这个可以工作,即使有两个返回语句
	sort.Sort(people(data.name))   // 这个不起作用。是否存在可以工作的版本?
	sort.Sort(people(data.salary)) // 这个不起作用。是否存在可以工作的版本?
	fmt.Println("\n\n\n")
	fmt.Print(data)
}

希望对你有帮助!

英文:

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 (
&quot;fmt&quot;
&quot;sort&quot;
)
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(&quot;%s: %g \n&quot;, 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 &lt; a[j].salary
return a[i].name &lt; 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 = &quot;Sheila Broflovski&quot;, &quot;Ben Affleck&quot;,
&quot;Mr. Hankey&quot;, &quot;Stan Marsh&quot;, &quot;Kyle Broflovski&quot;, &quot;Eric Cartman&quot;,
&quot;Kenny McCormick&quot;, &quot;Mr. Garrison&quot;, &quot;Matt Stone&quot;, &quot;Trey Parker&quot;
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] = &amp;a
data[1] = &amp;b
data[2] = &amp;c
data[3] = &amp;d
data[4] = &amp;e
data[5] = &amp;f
data[6] = &amp;g
data[7] = &amp;h
data[8] = &amp;i
data[9] = &amp;j
fmt.Println(&quot;\n\n\n&quot;)
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(&quot;\n\n\n&quot;)
fmt.Print(data)
}

答案1

得分: 2

一种常见的介绍排序方法的方式是使用描述排序条件的新类型。这里使用了byNamebySalary。然后可以使用sort.Sort(byName(data))进行排序。

以下是一些演示代码。Go语言在构建数据结构方面非常出色(例如,在表驱动测试中广泛使用),这也可以帮助构建人员数据。

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("按姓名排序")
    sort.Sort(byName(p))
    for _, x := range p {
        fmt.Println(*x)
    }
    fmt.Println("按薪水排序")
    sort.Sort(bySalary(p))
    for _, x := range p {
        fmt.Println(*x)
    }
}
英文:

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 &quot;fmt&quot;
import &quot;sort&quot;
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 &lt; 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 &lt; p[j].Salary }
func (p bySalary) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
func main() {
p := people{
{&quot;Sheila Broflovski&quot;, 82000},
{&quot;Ben Affleck&quot;, 74000},
{&quot;Mr. Hankey&quot;, 0},
{&quot;Stan Marsh&quot;, 400},
{&quot;Kyle Broflovski&quot;, 2500},
{&quot;Eric Cartman&quot;, 1000},
{&quot;Kenny McCormick&quot;, 4},
{&quot;Mr. Garrison&quot;, 34000},
{&quot;Matt Stone&quot;, 234000},
{&quot;Trey Parker&quot;, 234000},
}
fmt.Println(&quot;by name&quot;)
sort.Sort(byName(p))
for _, x := range p {
fmt.Println(*x)
}
fmt.Println(&quot;by salary&quot;)
sort.Sort(bySalary(p))
for _, x := range p {
fmt.Println(*x)
}
}

答案2

得分: 1

要使用标准库sort包实现第二个排序顺序,你需要定义一个帮助类型来实现sort.Interface接口。针对你的示例中的薪水情况,你可以按照以下方式进行操作:

type bySalary struct {
    people
}

func (s bySalary) Less(i, j int) bool {
    return s.people[i].salary < s.people[j].salary
}

在这里,sort.Interface所需的LenSwap方法来自嵌入的people切片,而我提供了一个替代的比较操作。要对people数组进行排序,现在可以调用:

sort.Sort(bySalary{data})

使用这种模式,你可以很容易地实现任意数量的额外排序键,而几乎没有重复的代码。

你可以在这里尝试这个示例:http://play.golang.org/p/kq3SuXMylT

英文:

To implement a second sort ordering with the standard library sort package, you will need to define a helper type that implements sort.Interface. To handle the salary case in your example, you could do something like the following:

type bySalary struct {
people
}
func (s bySalary) Less(i, j int) bool {
return s.people[i].salary &lt; s.people[j].salary
}

Here, the Len and Swap methods required by sort.Interface come from the embedded people slice, while I've provided a replacement comparison operation. To sort a people array, you can now call:

sort.Sort(bySalary{data})

Using this pattern, it is easy to implement as many additional sort keys as you need with very little duplicate code.

You can play with this example here: http://play.golang.org/p/kq3SuXMylT

huangapple
  • 本文由 发表于 2014年2月16日 14:52:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/21808237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定