


An article will guide you through the basic file operations of Go language
#Opening and closing files
package main import "os" func main() { //os.Open是只读模式 fileObj, err := os.Open("永不退缩.txt") if err != nil { panic(err) } //关闭文件 fileObj.Close() //一般情况下使用这种方式关闭文件 defer fileObj.Close() }
Note:If you are using Goland , when learning about file operations, try todon’t right-click to run, because the path to find the file is different
Compile as much as possible into a .exe file, so that the current directory is found
读取文件
读取指定大小
代码
func main() { //os.Open是只读模式 fileObj, err := os.Open("永不退缩.txt") if err != nil { panic(err) } var fileBytes = make([]byte,128) n, err := fileObj.Read(fileBytes) if err != nil { panic(err) } fmt.Println(string(fileBytes[:n])) //一般情况下使用这种方式关闭文件 defer fileObj.Close() }
执行结果
可以发现,根本就没有读取完,并且还乱码了。
读取整个文件
上述我们只读取了128个字节,一个中文3个字节,128/3
,除下标点符号,应该是40个左右,明显不够
那该怎么办才能读取所有呢???
上述我们只读取了128个字节,我们可以在读取128个字节啊
然后找个罐子将每次读取的都装进去,读取完,装完,完美
代码
func main() { //os.Open是只读模式 fileObj, err := os.Open("永不退缩.txt") if err != nil { panic(err) } //一般情况下使用这种方式关闭文件 defer fileObj.Close() var 罐子 []string var 每次读取字节 = make([]byte,128) for{ n, err := fileObj.Read(每次读取字节) //err == io.EOF表示读完了,一定要放在err != nil前面 if err == io.EOF { break } if err != nil { panic(err) } var 每次读取字符串 = string(每次读取字节[:n]) 罐子 = append(罐子,每次读取字符串) } fmt.Println(罐子) }
执行结果
会发现还是有乱码,这是为啥???
这是因为我们每次都是按照字节来读取一部分,一部分的,但是中文是3个字节,所有有时候可能切错了,就出现了乱码
那咋办呢?没办法,默认的读取文件就这么点功能,更多功能,尽在bufio
bufio
bufio
可以理解为原生file操作的一个加强版,更牛叉,功能更多
还是上述这个文件,看看如何完美读取
代码
func main() { //os.Open是只读模式 fileObj, err := os.Open("永不退缩.txt") if err != nil { panic(err) } //一般情况下使用这种方式关闭文件 defer fileObj.Close() //需要将文件对象传进去 reader := bufio.NewReader(fileObj) for{ //按行读取 row, err := reader.ReadString('\n')//参数是字符,不是字符串 if err == io.EOF { break } if err != nil { panic(err) } fmt.Printf("%v",row) } }
执行结果
读取整个文件
可能又有人说了,啊,星期八,我感觉还是麻烦,有没有更简单的办法读取文件,肯定有呀,一次性读取所有呢?答案是肯定的,此时需要用到ioutil
包。
代码
func main() { //os.Open是只读模式 bytes, err := ioutil.ReadFile("永不退缩.txt") if err != nil { panic(err) } fmt.Println(string(bytes)) }
执行结果
但是这种有个缺点,只能读取小文件,要是来个10G文件也这样玩,保证电脑死机!!!
写入文件
写入文件的话,我们就需要使用openFile
这个方法打开文件
在开始之前呢,先记一下下面几种模式
os.O_WRONLY
只写os.O_RDONLY
只读os.O_CREATE
如果文件不存在,则创建文件os.O_RDWR
可读可写os.O_TRUNC
清空os.O_APPEND
追加文件
写入字节和写入行
代码
func main() { fileObj, err := os.OpenFile("临时.txt", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) if err != nil { panic(err) } defer fileObj.Close() w_content := "我是临时表内容\n" //Write方法需要将字符串转成字节 fileObj.Write([]byte(w_content)) //WriteString直接写入字符串 fileObj.WriteString(w_content) }
执行结果
bufio写文件
代码
func main() { fileObj, err := os.OpenFile("临时.txt", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) if err != nil { panic(err) } defer fileObj.Close() w_content := "我是临时表内容\n" writer := bufio.NewWriter(fileObj) writer.Write([]byte(w_content)) writer.WriteString(w_content) //写完必须刷入 writer.Flush() }
执行结果
ioutil写文件
代码
func main() { w_content := "我是临时表内容\n" err := ioutil.WriteFile("临时.txt", []byte(w_content), 0666) if err != nil { panic(err) } }
执行结果
拷贝文件
拷贝文件,就是拷贝文件呗,A文件拷贝到B文件中
其实他的底层还是打开俩文件,把A文件内容写入到B文件中
代码
func main() { 原文件, err := os.Open("永不退缩.txt") if err != nil { panic(err) } defer 原文件.Close() 目标文件, err := os.OpenFile("临时.txt", os.O_WRONLY|os.O_CREATE, 0666) if err != nil { panic(err) } defer 目标文件.Close() //拷贝文件 io.Copy(目标文件,原文件) }
执行结果
总结
本次章节我们主要学习文件操作的相关知识,主要分为读取文件和写入文件
读取文件都有原生方法读写和bufio加强读写和ioutil一次性读写
推荐大家使用bufio这个包操作文件,带有缓冲功能,性能更好!!
The above is the detailed content of An article will guide you through the basic file operations of Go language. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...
