我必须在Golang中做加权随机,但我得到了一个错误:
multiple-value randutil.WeightedChoice() in single-value context代码:
package main
import "fmt"
import "github.com/jmcvetta/randutil"
func main() {
choices := make([]randutil.Choice, 0, 2)
choices = append(choices, randutil.Choice{1, "dg"})
choices = append(choices, randutil.Choice{2, "n"})
result := randutil.WeightedChoice(choices)
fmt.Println(choices)
}任何帮助都将深表感谢。
发布于 2016-09-18 14:45:27
The func WeightedChoice(choices []Choice) (Choice, error)
返回Choice, error,所以使用result, err := randutil.WeightedChoice(choices),就像下面这样的工作代码:
package main
import (
"fmt"
"github.com/jmcvetta/randutil"
)
func main() {
choices := make([]randutil.Choice, 0, 2)
choices = append(choices, randutil.Choice{1, "dg"})
choices = append(choices, randutil.Choice{2, "n"})
fmt.Println(choices) // [{1 dg} {2 n}]
result, err := randutil.WeightedChoice(choices)
if err != nil {
panic(err)
}
fmt.Println(result) //{2 n}
}输出:
[{1 dg} {2 n}]
{2 n}发布于 2016-09-18 06:42:29
WeightedChoice返回一个您没有在代码中确认的错误。
https://stackoverflow.com/questions/39551985
复制相似问题