Reputation: 157
I have to do weighted Random in Golang but I'm getting an error:
multiple-value randutil.WeightedChoice() in single-value context
Code:
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)
}
Any help will be deeply appreciated.
Upvotes: 0
Views: 4926
Reputation:
The func WeightedChoice(choices []Choice) (Choice, error)
returns Choice, error
, so use result, err := randutil.WeightedChoice(choices)
, like this working code:
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}
}
output:
[{1 dg} {2 n}]
{2 n}
Upvotes: 7
Reputation: 1580
WeightedChoice
returns an error you aren't acknowledging in your code.
Upvotes: 0