package main

import ( "fmt" "io/ioutil" "math/rand" "strings" "time" )

func main() { // 读取文件内容 content, err := ioutil.ReadFile("words.txt") if err != nil { fmt.Println("读取文件失败:", err) return }

// 将文件内容转换为字符串
words := strings.Split(string(content), "\n")

// 初始化随机数生成器
rand.Seed(time.Now().Unix())

// 随机获取一个单词
randomIndex := rand.Intn(len(words))
randomWord := words[randomIndex]

fmt.Println("随机获取的单词是:", randomWord)

}

用go语言存一些词语在txt文件里面并且可以随机读取其中的一个写出代码

原文地址: https://www.cveoy.top/t/topic/bSeR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录