当前位置:网站首页>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()
}
边栏推荐
- ABP框架之——数据访问基础架构(下)
- What are the risks of opening a fund account? Is it safe to open an account
- R语言使用timeROC包计算无竞争情况下的生存资料多时间AUC值、使用cox模型、并添加协变量、可视化无竞争情况下的生存资料多时间ROC曲线
- XML
- golang日期时间time包代码示例: 根据生日获取年龄、生肖、星座
- stylegan3:alias-free generative adversarial networks
- js 递归json树 根据 子id 查 父id
- Amadis发布OLA支付处理标准
- SaaS cloud tool, a sharp tool for change under the industrial Internet
- golang数据类型图
猜你喜欢

融云:让银行轻松上“云”

万字攻略,详解腾讯面试(T1-T9)核心技术点,面试题整理

创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋

Solution: in the verification phase, the first batch does not report errors, and the second batch reports CUDA exceeded errors

阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一文搞定

Server deployment and instructions
Drag the child file to the upper level

Quartz

【解决】npm WARN config global `--global`, `--local` are deprecated. Use `--location=global`

How to configure PostgreSQL data source on SSRs page
随机推荐
【历史上的今天】6 月 23 日:图灵诞生日;互联网奠基人出生;Reddit 上线
Introduction to asynccontext
ADB key name, key code number and key description comparison table
Amadis发布OLA支付处理标准
Does the enterprise want to use the MES system? These conditions have to be met
ABP框架之——数据访问基础架构(下)
Memory consistency and cache consistency
How did Tencent's technology bulls complete the overall cloud launch?
SaaS cloud tool, a sharp tool for change under the industrial Internet
stylegan1: a style-based henerator architecture for gemerative adversarial networks
Pytorch: saving and exporting models
PageHelper faces the paging problem of complex service data processing
Uniapp sends picture messages to Tencent instant messaging Tim
Ten thousand words introduction, detailed explanation of the core technical points of Tencent interview (t1-t9), and arrangement of interview questions
总结一下购买阿里云服务器的经验
CA authentication and issuance of revocation certificates
Web篇_01 了解web開發
【TcaplusDB知识库】Tmonitor后台一键安装介绍(一)
Golang对JSON文件的写操作
【TcaplusDB知识库】TcaplusDB Tmonitor模块架构介绍