package main import ( "io/IoUtil" "strings" "fmt" ) func main() { file_bytes,err := IoUtil.ReadFile("file.txt") if err != nil { panic(err) } lines := strings.Split(string(file_bytes),"\n") fmt.Println(lines) }
方法2:使用bufio的scanner,这种方法支持每行最多65536个字符
package main import ( "os" "bufio" "fmt" ) func main() { f,err := os.Open("file.txt") if err != nil { panic(err) } defer f.Close() scanner := bufio.NewScanner(f) for scanner.Scan() { fmt.Println(scanner.Text()) } if err := scanner.Err(); err != nil { panic(err) } }