mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 05:42:01 +08:00
28 lines
384 B
Go
28 lines
384 B
Go
package model
|
|
|
|
import (
|
|
"random-api-go/config"
|
|
"sync"
|
|
)
|
|
|
|
type URLSelector struct {
|
|
URLs []string
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func NewURLSelector(urls []string) *URLSelector {
|
|
return &URLSelector{
|
|
URLs: urls,
|
|
}
|
|
}
|
|
|
|
func (us *URLSelector) GetRandomURL() string {
|
|
us.mu.Lock()
|
|
defer us.mu.Unlock()
|
|
|
|
if len(us.URLs) == 0 {
|
|
return ""
|
|
}
|
|
return us.URLs[config.RNG.Intn(len(us.URLs))]
|
|
}
|