Go 读取文件的几种方法

ioutil.ReadFile

const filename = "abc.txt"
if contents, err := ioutil.ReadFile(filename); err != nil {
	fmt.Println(err)
} else {
	fmt.Printf("%s\n", contents)
}

bufio.NewScanner

file, err := os.Open(filename)
if err != nil{
	panic(err)
}

scanner := bufio.NewScanner(file)
for scanner.Scan() {
	fmt.Println(scanner.Text())
}

你可能感兴趣的:(golang)