mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 03:55:28 +08:00
@@ -246,7 +246,7 @@ func main () {
|
||||
除了文件句柄,我们还需要 `bufio` 的 `Writer`。我们以只写模式打开文件 `output.dat`,如果文件不存在则自动创建:
|
||||
|
||||
```go
|
||||
outputFile, outputError := os.OpenFile(“output.dat”, os.O_WRONLY|os.O_ CREATE, 0666)
|
||||
outputFile, outputError := os.OpenFile(“output.dat”, os.O_WRONLY|os.O_CREATE, 0666)
|
||||
```
|
||||
|
||||
可以看到,`OpenFile` 函数有三个参数:文件名、一个或多个标志(使用逻辑运算符“|”连接),使用的文件权限。
|
||||
@@ -254,7 +254,7 @@ outputFile, outputError := os.OpenFile(“output.dat”, os.O_WRONLY|os.O_ CREAT
|
||||
我们通常会用到以下标志:
|
||||
|
||||
- `os.O_RDONLY`:只读
|
||||
- `os.WRONLY`:只写
|
||||
- `os.O_WRONLY`:只写
|
||||
- `os.O_CREATE`:创建:如果指定文件不存在,就创建该文件。
|
||||
- `os.O_TRUNC`:截断:如果指定文件已存在,就将该文件的长度截为0。
|
||||
|
||||
|
Reference in New Issue
Block a user