当前位置:网站首页>Golang write file code example
Golang write file code example
2022-06-23 16:24:00 【Learn programming notes】
Go Linguistic os There is a OpenFile function , The prototype is shown below :
func OpenFile(name string, flag int, perm FileMode) (file *File, err error)
among name Is the file name of the file , If you are not running under the current path, you need to add a specific path ;flag Is the processing parameter of the file , by int type , The specific values may vary depending on the system , But the effect is the same .
Here are some common flag File processing parameters : O_RDONLY: Open file in read-only mode ; O_WRONLY: Write only mode open file ; O_RDWR: Open file in read-write mode ; O_APPEND: Attach data to the end of the file when writing ( Additional ); O_CREATE: If it doesn't exist, a new file will be created ; O_EXCL: and O_CREATE In combination with , The file must not exist , Otherwise an error is returned ; O_SYNC: When doing a series of write operations , Every time I have to wait for the last time I/O When the operation is finished, proceed ; O_TRUNC: If possible , Empty file on open .
【 Example 1】: Create a new file golang.txt, And write in it 5 sentence “http://c.biancheng.net/golang/”
- Case study 1.go
1.go
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
// Create a new file , Write content 5 sentence “http://c.biancheng.net/golang/”
filePath := "D:/goLang/github/code/golang.txt"
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(" File opening failure ", err)
}
// Shut down in time file Handle
defer file.Close()
// When writing a file , Use cached *Writer
/* func NewWriter func NewWriter(w io.Writer) *Writer NewWriter Create a buffer with a default size 、 write in w Of *Writer. */
write := bufio.NewWriter(file)
for i := 0; i < 5; i++ {
write.WriteString("http://c.biancheng.net/golang/ \n")
}
// Flush Write the cached file to the file
write.Flush()
}
边栏推荐
- [tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (I)
- js 对象 使用小技巧
- stylegan2:analyzing and improving the image quality of stylegan
- 提高效率 Or 增加成本,开发人员应如何理解结对编程?
- Web容器是怎样给第三方插件做初始化工作的
- 腾讯的技术牛人们,是如何完成全面上云这件事儿的?
- 多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中
- Openresty Foundation
- 规避这六大难点,成功实施MES系统
- Improving efficiency or increasing costs, how should developers understand pair programming?
猜你喜欢

二分查找法思路分析

Thread pool

如何让销售管理更高效?

Summarize the experience of purchasing Alibaba cloud servers

Server deployment and instructions

Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022

openGauss数据库源码解析系列文章—— 密态等值查询技术详解(上)

XML

XML

进阶开发阶段-势若悬丝的加粗开始. 现在的一小步,明年的一大步
随机推荐
Readimg: read picture to variable variable variable
Advanced development stage - the thickening of potential suspension wire begins A small step now, a big step next year
How does the web container initialize third-party plug-ins
Thinking analysis of binary search method
[tcapulusdb knowledge base] Introduction to new models of tcapulusdb
阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一文搞定
R语言ggplot2可视化水平箱图(Horizontal boxplot with coord_flip)、并添加抖动数据点显示分布情况(jittered points)
Log4j log integration and configuration details
A tour of gRPC:01 - 基础理论
PageHelper faces the paging problem of complex service data processing
[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (II)
Block, non block, multiplexing, synchronous, asynchronous, bio, NiO, AIO
Tips for accelerating file transfer between windows remote desktop connections
六石编程学:运用之妙,存乎一心
Does the enterprise want to use the MES system? These conditions have to be met
golang goroutine、channel、time代码示例
CoAtNet: Marrying Convolution and Attention for All Data Sizes翻译
Web篇_01 了解web开发
2022九峰小学(光谷第二十一小学)生源摸底
Uniapp sends picture messages to Tencent instant messaging Tim