Skip to content

Commit

Permalink
Fix use of rand.Intn
Browse files Browse the repository at this point in the history
The example uses rand.Intn(4) on a slice of len 5, meaning the last element (e.g. "blue") can never be returned. rand.Intn should be called with the len of the slice.
  • Loading branch information
bancron authored Jun 21, 2019
1 parent 337e902 commit 4bb7315
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions example/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ func (h *randomHaberdasher) MakeHat(ctx context.Context, size *example.Size) (*e
if size.Inches <= 0 {
return nil, twirp.InvalidArgumentError("Inches", "I can't make a hat that small!")
}
return &example.Hat{
colors := []string{"white", "black", "brown", "red", "blue"}
names := []string{"bowler", "baseball cap", "top hat", "derby"}
return &haberdasher.Hat{
Size: size.Inches,
Color: []string{"white", "black", "brown", "red", "blue"}[rand.Intn(4)],
Name: []string{"bowler", "baseball cap", "top hat", "derby"}[rand.Intn(3)],
Color: colors[rand.Intn(len(colors))],
Name: names[rand.Intn(len(names))],
}, nil
}

Expand Down

0 comments on commit 4bb7315

Please sign in to comment.