需求
在某些场景下,需要根据给定值,返回一批特定的结果,在这种情况下,需要返回切片中的某一个特定的值。
实现
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
userAgentSlice := []string{
"Podcasts/1430.46+CFNetwork/1125.2+Darwin/19.4.0",
"Spotify/1.0",
"PocketCasts/1.0+(Pocket+Casts+Feed+Parser;++http://pocketcasts.com/)",
"iTMS",
"AirPodcasts/1440.4+CFNetwork/1126+Darwin/19.5.0",
"Tentacles,+Like+iTunes",
}
rand.Seed(time.Now().Unix())
fmt.Println(userAgentSlice[rand.Intn(len(userAgentSlice))])
}
Code language: JavaScript (javascript)