获取文件内容为string类型
(filePath string)
| 38 | |
| 39 | // 获取文件内容为string类型 |
| 40 | func ReadFileToString(filePath string) (string, error) { |
| 41 | file, err := os.Open(filePath) |
| 42 | if err != nil { |
| 43 | return "", fmt.Errorf("could not open file %s: %w", filePath, err) |
| 44 | } |
| 45 | defer file.Close() |
| 46 | |
| 47 | var content string |
| 48 | scanner := bufio.NewScanner(file) |
| 49 | for scanner.Scan() { |
| 50 | content = content + scanner.Text() + "\n" |
| 51 | } |
| 52 | |
| 53 | if err := scanner.Err(); err != nil { |
| 54 | return "", fmt.Errorf("error reading file %s: %w", filePath, err) |
| 55 | } |
| 56 | |
| 57 | return content, nil |
| 58 | } |
| 59 | |
| 60 | // 获取文件内容为[]string类型 |
| 61 | func ReadFileToSlice(filePath string) ([]string, error) { |
nothing calls this directly
no outgoing calls
no test coverage detected