Skip to content

Instantly share code, notes, and snippets.

@monkrus
Last active July 9, 2022 18:41
Show Gist options
  • Save monkrus/0277733b06a2de3b5013c688deaa4894 to your computer and use it in GitHub Desktop.
Save monkrus/0277733b06a2de3b5013c688deaa4894 to your computer and use it in GitHub Desktop.
Golang program to read/write/rewrite the files
// Golang program to read and write the files
package main
// importing the packages
import (
"fmt"
"io/ioutil"
"log"
"os"
)
func CreateFile() {
// fmt package implements formatted
// I/O and has functions like Printf
// and Scanf
fmt.Printf("Writing to a file in Go lang\n")
// in case an error is thrown it is received
// by the err variable and Fatalf method of
// log prints the error message and stops
// program execution
file, err := os.Create("test.txt")
if err != nil {
log.Fatalf("failed creating file: %s", err)
}
// Defer is used for purposes of cleanup like
// closing a running file after the file has
// been written and main function has
// completed execution
defer file.Close()
// len variable captures the length
// of the string written to the file.
len, err := file.WriteString("Welcome to the demonstration !")
if err != nil {
log.Fatalf("failed writing to file: %s", err)
}
// Name() method returns the name of the
// file as presented to Create() method.
fmt.Printf("\nFile Name: %s", file.Name())
fmt.Printf("\nLength: %d bytes", len)
}
func ReadFile() {
fmt.Printf("\n\nReading a file in Go lang\n")
fileName := "test.txt"
// The ioutil package contains inbuilt
// methods like ReadFile that reads the
// filename and returns the contents.
data, err := ioutil.ReadFile("test.txt")
if err != nil {
log.Panicf("failed reading data from file: %s", err)
}
fmt.Printf("\nFile Name: %s", fileName)
fmt.Printf("\nSize: %d bytes", len(data))
fmt.Printf("\nData: %s", data)
}
func UpdateFile() {
file, err := os.OpenFile("test.txt", os.O_RDWR|os.O_TRUNC, 0)
if err != nil {
log.Fatalf("failed writing to file: %s", err)
}
len, err := file.WriteString("Welcome to IB!")
if err != nil {
log.Fatalf("failed writing to file: %s", err)
}
data, err := ioutil.ReadFile("test.txt")
if err != nil {
log.Panicf("failed reading data from file: %s", err)
}
fmt.Printf("\nData: %s", data)
// Name() method returns the name of the
// file as presented to Create() method.
fmt.Printf("\nFile Name: %s", file.Name())
fmt.Printf("\nLength: %d bytes", len)
}
// main function
func main() {
CreateFile()
ReadFile()
UpdateFile()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment