Go (golang) Tutorials - Buffered File I/O

Published: 07 April 2018
on channel: Ambasoft Java
5,560
86

#golang #go #input #output #file

File I/O - Buffered I/O
--------------------------
= Writing Data to File
= Reading from a File
= Appending to a File

bufwritedemo.go
----------------
package main
import ("os"
"bufio"
)
func main(){
file,err:=os.Create("newfile")
if err!=nil {
panic(err)
}
buf:=bufio.NewWriter(file) //Create a buffer for the os File
buf.WriteString("Writing Data to Buffer\n") //Write data to buffer
buf.Flush() //clears the buffer data and writes to the file
file.Close()
}

bufreaddemo.go
---------------
package main
import ("os"
"bufio"
"fmt"
)
func main(){
file,_:=os.Open("newfile")
reader:=bufio.NewReader(file)
data,_:=reader.Peek(50)
fmt.Println(string(data))
file.Close()
}

appenddemo.go
--------------
package main
import "os"
func main(){
//Opens an existing file with APPEND mode and Read/Write permission
file,err:=os.OpenFile("newfile",os.O_APPEND|os.O_WRONLY,0644)
if err !=nil {
panic(err)
}
file.WriteString("Trying to append\n")
file.Close()
}


Watch video Go (golang) Tutorials - Buffered File I/O online, duration hours minute second in high quality that is uploaded to the channel Ambasoft Java 07 April 2018. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 5,560 times and liked it 86 visitors.