https://github.com/lestrrat-go/choose
Choose randomized element(s) from containers in a generic way
https://github.com/lestrrat-go/choose
Last synced: 4 months ago
JSON representation
Choose randomized element(s) from containers in a generic way
- Host: GitHub
- URL: https://github.com/lestrrat-go/choose
- Owner: lestrrat-go
- License: mit
- Created: 2022-01-06T07:34:10.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-06T13:34:29.000Z (over 3 years ago)
- Last Synced: 2025-01-09T03:41:43.781Z (6 months ago)
- Language: Go
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# choose
`githbu.com/lestrrat-go/choose` is a simple tool that allows users to choose random
elements out of a container. It uses the new type parameters introduced in go 1.18# Slice
Initialize a choose.SliceChooser:
```go
src := []int{....}
c := choose.Slice[int](src)
```## Choose a random element from a slice
```go
elem := c.One()
```## Choose N random elements from a slice
```go
elems := c.N(3)
```# Map
Initialize a choose.MapChooser:
```go
src := map[string]int{...}
c := choose.Map[string,int](src)
```## Choose a random element (as choose.MapElement) from a map
```go
elem := c.One()
fmt.Printf("%s -> %v", elem.Key, elem.Value)
```## Choose N random elements (as []choose.MapElement) from a map
```go
elem := c.N(3)
```