You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
852 B
Go

3 years ago
/**
* @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]
}