os包

const (
	// Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified.
	O_RDONLY int = syscall.O_RDONLY // open the file read-only. 只读
	O_WRONLY int = syscall.O_WRONLY // open the file write-only. 只写
	O_RDWR   int = syscall.O_RDWR   // open the file read-write. 读写
	// The remaining values may be or'ed in to control behavior.
	O_APPEND int = syscall.O_APPEND // append data to the file when writing. 追加
	O_CREATE int = syscall.O_CREAT  // create a new file if none exists. 创建
	O_EXCL   int = syscall.O_EXCL   // used with O_CREATE, file must not exist. 文件是否存在
	O_SYNC   int = syscall.O_SYNC   // open for synchronous I/O. 同步IO
	O_TRUNC  int = syscall.O_TRUNC  // truncate regular writable file when opened. 如果可能,清空文件
)

读取文件

os.Open会获得一个文件指针。

var file, err = os.Open("content.text")
if err != nil {
	fmt.Println(err)
}
fmt.Println(file)
defer file.Close()  // 关闭文件句柄
if err != nil {
	fmt.Println(err)
}
reader := bufio.NewReader(file)
for {
	str, err := reader.ReadString('\n')

	if err == io.EOF {
		break
	}
	fmt.Println(str)
}