func main()create the reasons slice and append reasons to itreasons := make([]string,0)reasons = append(reasons, "Locked out", "ipes broke", "Food poisoning", "Not feeling well")message := fmt.Sprint("Gonna work from home...",pick a random reason )}# W5 A7 I3 y% H k
问题: 5 J `, N" m+ n: F2 }* p是否有内置函数可以通过执行来执行选择随机原因部分来帮我?: Z, Q" s7 ~1 G6 y. t6 _6 S
- [. A! S& e& E' O) g 解决方案: % l3 a# Q& d9 U$ U9 R
使用功能,Intn从rand在包中选择随机指数。 6 {2 {9 V' a R
import ( "math/rand" "time")// ...rand.Seed(time.Now().Unix() // initialize global pseudo random generatormessage := fmt.Sprint("Gonna work from home...",reasons[rand.Intn(len(reasons))])0 ~ K+ g! `/ ]- z& w" a