package utils import ( "fmt" "log" "os" ) func SaveCache2File(cacheStr string, fileName string) error { // 打开文件,如果文件不存在则创建 file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666) if err != nil { log.Println("Error opening file:", err) return err } defer file.Close() // 将变量写入文件 _, err = file.WriteString(cacheStr) if err != nil { log.Println("Error writing to file:", err) } return err } func ReadCache2File(fileName string) (string, error) { // 打开文件 file, err := os.Open(fileName) if err != nil { log.Println("Error opening file:", err) return "", err } defer file.Close() // 读取文件内容 var content string _, err = fmt.Fscanf(file, "%s", &content) if err != nil { log.Println("Error reading from file:", err) } return content, err } func FileExists(filePath string) bool { _, err := os.Stat(filePath) if err != nil { if os.IsNotExist(err) { return false } } return true }