Open a File on your local storage media

Assume we have the following file, located in the same folder as Go main program that open a file.

Demofile.txt

Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!

package main

import (
	"fmt"
	"io/ioutil"
)

func main() {
	content, err := ioutil.ReadFile("demofile.txt")
	if err != nil {
		fmt.Println("Error reading file")
	}
	fmt.Print(string(content))
}

Output:

Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!

Categories: Mini-Programs