// Open opens the named file for reading. If successful, methods on// the returned file can be used for reading; the associated file// descriptor has mode O_RDONLY.// If there is an error, it will be of type *PathError.funcOpen(name string)(*File,error){returnOpenFile(name, O_RDONLY,0)}
// Read reads up to len(b) bytes from the File.// It returns the number of bytes read and any error encountered.// At end of file, Read returns 0, io.EOF.func(f *File)Read(b []byte)(n int, err error){if err := f.checkValid("read"); err !=nil{return0, err
}
n, e := f.read(b)return n, f.wrapErr("read", e)}
// NewReader returns a new Reader whose buffer has the default size.funcNewReader(rd io.Reader)*Reader {returnNewReaderSize(rd, defaultBufSize)}// ReadString reads until the first occurrence of delim in the input,// returning a string containing the data up to and including the delimiter.// If ReadString encounters an error before finding a delimiter,// it returns the data read before the error and the error itself (often io.EOF).// ReadString returns err != nil if and only if the returned data does not end in// delim.// For simple uses, a Scanner may be more convenient.func(b *Reader)ReadString(delim byte)(string,error){
bytes, err := b.ReadBytes(delim)returnstring(bytes), err
}
// ReadFile reads the file named by filename and returns the contents.// A successful call returns err == nil, not err == EOF. Because ReadFile// reads the whole file, it does not treat an EOF from Read as an error// to be reported.funcReadFile(filename string)([]byte,error){
f, err := os.Open(filename)if err !=nil{returnnil, err
}defer f.Close()// It's a good but not certain bet that FileInfo will tell us exactly how much to// read, so let's try it but be prepared for the answer to be wrong.var n int64= bytes.MinRead
if fi, err := f.Stat(); err ==nil{// As initial capacity for readAll, use Size + a little extra in case Size// is zero, and to avoid another allocation after Read has filled the// buffer. The readAll call will read into its allocated internal buffer// cheaply. If the size was wrong, we'll either waste some space off the end// or reallocate as needed, but in the overwhelmingly common case we'll get// it just right.if size := fi.Size()+ bytes.MinRead; size > n {
n = size
}}returnreadAll(f, n)}
// OpenFile is the generalized open call; most users will use Open// or Create instead. It opens the named file with specified flag// (O_RDONLY etc.) and perm (before umask), if applicable. If successful,// methods on the returned File can be used for I/O.// If there is an error, it will be of type *PathError.funcOpenFile(name string, flag int, perm FileMode)(*File,error){
testlog.Open(name)returnopenFileNolog(name, flag, perm)}// 第一個參數爲文件名// 接收字符串// 第二個參數爲打開的方式// 見下表// 第三個參數爲權限// 同linux權限
// WriteFile writes data to a file named by filename.// If the file does not exist, WriteFile creates it with permissions perm;// otherwise WriteFile truncates it before writing.funcWriteFile(filename string, data []byte, perm os.FileMode)error{
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)if err !=nil{return err
}
n, err := f.Write(data)if err ==nil&& n <len(data){
err = io.ErrShortWrite
}if err1 := f.Close(); err ==nil{
err = err1
}return err
}