GO語言"os"包中"OpenFile"函數的用法及代碼示例。
用法:
func OpenFile(name string, flag int, perm FileMode)(*File, error)
OpenFile是廣義的公開調用;大多數用戶會改用 Open 或 Create 。它使用指定的標誌(O_RDONLY 等)打開命名文件。如果文件不存在,並且傳遞了 O_CREATE 標誌,則使用模式 perm(在 umask 之前)創建它。如果成功,則返回的 File 上的方法可用於 I/O。如果有錯誤,它將是 *PathError 類型。
例子:
package main
import (
"log"
"os"
)
func main() {
f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
}
示例(附加):
package main
import (
"log"
"os"
)
func main() {
// If the file doesn't exist, create it, or append to the file
f, err := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write([]byte("appended some data\n")); err != nil {
f.Close() // ignore error; Write error takes precedence
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
}
相關用法
- GO OnesCount8用法及代碼示例
- GO Once用法及代碼示例
- GO OnesCount16用法及代碼示例
- GO OnesCount32用法及代碼示例
- GO OnesCount64用法及代碼示例
- GO OnesCount用法及代碼示例
- GO PutUvarint用法及代碼示例
- GO Scanner.Scan用法及代碼示例
- GO LeadingZeros32用法及代碼示例
- GO NewFromFiles用法及代碼示例
- GO Regexp.FindString用法及代碼示例
- GO Time.Sub用法及代碼示例
- GO Regexp.FindAllIndex用法及代碼示例
- GO Encode用法及代碼示例
- GO ResponseRecorder用法及代碼示例
- GO Value用法及代碼示例
- GO StreamWriter用法及代碼示例
- GO Fscanln用法及代碼示例
- GO Values.Get用法及代碼示例
- GO NumError用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 OpenFile。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。