Home > Article > Backend Development > How to read files in golang
How does golang read files?
Four ways to read files in golang
Read files
The read files are placed in file/test: that is, the test file under the file package , write more files in it
Reading file method one: Use ioutil.ReadFile to read directly from the file into []byte
func Read0() (string){ f, err := ioutil.ReadFile("file/test") if err != nil { fmt.Println("read fail", err) } return string(f) }
Reading file method two: First read from the file to file In, when reading from file to buf, buf is appended to the final []byte
func Read1() (string){ //获得一个file f, err := os.Open("file/test") if err != nil { fmt.Println("read fail") return "" } //把file读取到缓冲区中 defer f.Close() var chunk []byte buf := make([]byte, 1024) for { //从file读取到buf中 n, err := f.Read(buf) if err != nil && err != io.EOF{ fmt.Println("read buf fail", err) return "" } //说明读取结束 if n == 0 { break } //读取到最终的缓冲区中 chunk = append(chunk, buf[:n]...) } return string(chunk) //fmt.Println(string(chunk)) }
Reading file method three: first read from file to file, then read from file to Reader, from Reader Read buf, buf is finally appended to []byte
//先从文件读取到file, 在从file读取到Reader中,从Reader读取到buf, buf最终追加到[]byte,这个排第三 func Read2() (string) { fi, err := os.Open("file/test") if err != nil { panic(err) } defer fi.Close() r := bufio.NewReader(fi) var chunks []byte buf := make([]byte, 1024) for { n, err := r.Read(buf) if err != nil && err != io.EOF { panic(err) } if 0 == n { break } //fmt.Println(string(buf)) chunks = append(chunks, buf...) } return string(chunks) //fmt.Println(string(chunks)) }
Fourth way to read files: read into file, and then use ioutil to read the file directly into []byte
//读取到file中,再利用ioutil将file直接读取到[]byte中, 这是最优 func Read3() (string){ f, err := os.Open("file/test") if err != nil { fmt.Println("read file fail", err) return "" } defer f.Close() fd, err := ioutil.ReadAll(f) if err != nil { fmt.Println("read to fd fail", err) return "" } return string(fd) }
Reading speed comparison
After my test, the reading speed ranking of these four methods is: the former is superior
Method four> Method one> Method three> Method four
Write file
Write file method one: use io.WriteString to write the file
func Write0() { fileName := "file/test1" strTest := "测试测试" var f *os.File var err error if CheckFileExist(fileName) { //文件存在 f, err = os.OpenFile(fileName, os.O_APPEND, 0666) //打开文件 if err != nil{ fmt.Println("file open fail", err) return } }else { //文件不存在 f, err = os.Create(fileName) //创建文件 if err != nil { fmt.Println("file create fail") return } } //将文件写进去 n, err1 := io.WriteString(f, strTest) if err1 != nil { fmt.Println("write error", err1) return } fmt.Println("写入的字节数是:", n) }
Write file method two: use ioutil.WriteFile to write the file
func Write1() { fileName := "file/test2" strTest := "测试测试" var d = []byte(strTest) err := ioutil.WriteFile(fileName, d, 0666) if err != nil { fmt.Println("write fail") } fmt.Println("write success") }
The third way to write a file: Use File(Write,WriteString) to write a file
func Write2() { fileName := "file/test3" strTest := "测试测试" var d1 = []byte(strTest) f, err3 := os.Create(fileName) //创建文件 if err3 != nil{ fmt.Println("create file fail") } defer f.Close() n2, err3 := f.Write(d1) //写入文件(字节数组) fmt.Printf("写入 %d 个字节n", n2) n3, err3 := f.WriteString("writesn") //写入文件(字节数组) fmt.Printf("写入 %d 个字节n", n3) f.Sync() }
The fourth way to write a file: Use bufio.NewWriter to write a file
func Write3() { fileName := "file/test3" f, err3 := os.Create(fileName) //创建文件 if err3 != nil{ fmt.Println("create file fail") } w := bufio.NewWriter(f) //创建新的 Writer 对象 n4, err3 := w.WriteString("bufferedn") fmt.Printf("写入 %d 个字节n", n4) w.Flush() f.Close() }
Check whether the file exists:
func CheckFileExist(fileName string) bool { _, err := os.Stat(fileName) if os.IsNotExist(err) { return false } return true }
For more golang knowledge, please pay attention to the golang tutorial column on the PHP Chinese website.
The above is the detailed content of How to read files in golang. For more information, please follow other related articles on the PHP Chinese website!