- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to read entire text from the existing file
In this Golang article we will use ReadFile function from the io/ioutil package, NewScanner function from the bufio packageand as well as NewReader function from the former package respectively to read entire text from the xis=sting file.
Syntax
bufio.NewReader()
This function belongs to Go's bufio package. The primary goal of this function is to read the data in larger chunks instead of line by line and store in buffer. The io.reader and buffer size are passed as arguments in this function.
os.Open()
This function is a part of os package. It is used to open a file to read. It takes one input i.e. the filename which will be opened.
ioutil.ReadFile()
This function is available in the ioutil package and is used to read the contents of a file with filename as an input in the function.
bufio.NewScanner()
This function is a part of the bufio package. It is used to create a scanner object to read the data from the file.
Algorithm
Import the required packages in the program
Create a main function
Read the content of the file using inbuilt-in functions
Print the error if the content is not read properly
Example 1
In this Example we will write a go language program to read the entire text from the existing file by using the ReadFile() function present in the ioutil package. This function reads the entire contents of a file and returns it as a slice of bytes.
package main import ( "fmt" "io/ioutil" ) func main() { data, err := ioutil.ReadFile("file.txt") if err != nil { fmt.Println("Error reading file:", err) return } fmt.Println(string(data)) }
Output
The dictionary says it was Thomas Edison who put hello into common usage. He urged the people who used his phone to say "hello" when answering. His rival, Alexander Graham Bell, thought the better word was "ahoy."
Example 2
In this Example we will write a go language program to read the entire text from the existing file by using the functions present in os and bufio packages.
package main import ( "bufio" "fmt" "os" ) func main() { file, err := os.Open("file.txt") if err != nil { fmt.Println("Error opening file:", err) return } defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { fmt.Println(scanner.Text()) } if err := scanner.Err(); err != nil { fmt.Println("Error reading file:", err) } }
Output
The dictionary says it was Thomas Edison who put hello into common usage. He urged the people who used his phone to say "hello" when answering. His rival, Alexander Graham Bell, thought the better word was "ahoy."
Example3
In this Example we will write the go language program to read the entire text from the file by using the NewReaderfunction.This method provides more control over the reading process than the previous methods.
package main import ( "bufio" "fmt" "os" ) func main() { file, err := os.Open("file.txt") if err != nil { fmt.Println("Error opening file:", err) return } defer file.Close() reader := bufio.NewReader(file) data, err := reader.ReadString('\n') if err != nil { fmt.Println("Error reading file:", err) return } fmt.Println(data) }
Output
The dictionary says it was Thomas Edison who put hello into common usage. He urged the people who used his phone to say "hello" when answering. His rival, Alexander Graham Bell, thought the better word was "ahoy."
Conclusion
We have successfully compiled and executed a go language program to read the entire text from the existing file along with the Examples. in the first Example we are using ReadFile() function while in the second and third Example we are using NewScanner() and NewReader() function respectively to implement the result.