MCPcopy Create free account
hub / github.com/cnshym/httpgo / ReadFileToString

Function ReadFileToString

pkg/utils/file.go:40–58  ·  view source on GitHub ↗

获取文件内容为string类型

(filePath string)

Source from the content-addressed store, hash-verified

38
39// 获取文件内容为string类型
40func 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类型
61func ReadFileToSlice(filePath string) ([]string, error) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected