Golang(Go语言)读取文件基本用法

来源:互联网 发布:淘宝帐号个人昵称大全 编辑:程序博客网 时间:2024/06/11 10:37
需要用到os和bufio包
import "os"
import "bufio"
用os.Open打开一个文件,用bufio.NewReader来读取文件
file, err := os.Open("input.dat")
if err!= nil {
fmt.Println("failed to open")
return
}
defer file.Close()
reader := bufio.NewReader(file)
for {
str, err := reader.ReadString('\n') //每次读取一行
if err!= nil {
break // 读完或发生错误
}
fmt.Printf(str)
}


1 0
原创粉丝点击