/** * @Author: jager * @Email: lhj168os@gmail.com * @File: txt * @Date: 2021/12/31 3:01 下午 * @package: soup * @Version: v1.0.0 * * @Description: * */ package soup import ( "bufio" "io" "math/rand" "os" "strings" "github.com/jageros/hawox/logx" ) var contents []string func InitFromFile(path string) error { file, err := os.OpenFile(path, os.O_RDWR, 0666) if err != nil { return err } defer file.Close() buf := bufio.NewReader(file) for { line, err := buf.ReadString('\n') line = strings.TrimSpace(line) contents = append(contents, line) if err != nil { if err == io.EOF { logx.Infof("Initialize contents successful count=%d", len(contents)) break } else { return err } } } return nil } func getContent() string { index := rand.Intn(len(contents)) return contents[index] }