<< Back to GO
Golang File Handling
Handle files with the bufio type, NewScanner, Scan and Text. Read the lines of a text file.File. A file contains many lines. In Go we can use the bufio package to read in all the lines in this file inside a loop. With Scan and Text we get a string for each line.
With bufio, we have many helpful methods for handling text files. Newlines are handled automatically. The classes are easy to use with some practice.
First example. This program opens a file on the disk. Please change the path argument to os.Open to a file that exists on your computer. Errors are not handled here.
Open: This returns a *File descriptor. We can pass the result of Open() to the bufio.NewScanner method.
NewScanner: This creates a new *Scanner. The File we pass to this method is accessed through its Reader interface.
Scan: This advances to the next part of the file and returns true if there is more data. And text() creates a string from the current line.
Golang program that uses bufio, reads text file
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
// Open the file.
f, _ := os.Open("C:\\programs\\file.txt")
// Create a new Scanner for the file.
scanner := bufio.NewScanner(f)
// Loop over all lines in the file and print them.
for scanner.Scan() {
line := scanner.Text()
fmt.Println(line)
}
}
Output
Carrot
Bird
Fish
Turnip
Contents of file.txt
Carrot
Bird
Fish
Turnip
ScanWords. A file sometimes contains words. We can get each word separately as a string with the Scan() and Text() methods. First we must call Split.
Tip: The Split method here just sets the splitting method for the scanner. It influences the behavior of Scan().
Golang program that uses bufio.ScanWords, gets words
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
f, _ := os.Open("C:\\programs\\file.txt")
scanner := bufio.NewScanner(f)
// Set the Split method to ScanWords.
scanner.Split(bufio.ScanWords)
// Scan all words from the file.
for scanner.Scan() {
line := scanner.Text()
fmt.Println(line)
}
}
Output
a
commodius
vicus
of
recirculation
Contents of file.txt
a commodius vicus of recirculation
Read entire file. With ioutil.ReadAll we can get the entire contents of a file in a byte slice or a string. We must import "io/ioutil" and then call ioutil.ReadAll on a reader.
NewReader: We use bufio.NewReader to create a buffered text file reader. We can then pass this to ReadAll.
ReadAll: This func returns a byte slice. We can convert the byte slice into a string—this makes it easier to display.
Golang program that uses ioutil.ReadAll
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
)
func main() {
// Open a file.
f, _ := os.Open("C:\\programs\\file.txt")
// Use bufio.NewReader to get a Reader.
// ... Then use ioutil.ReadAll to read the entire content.
reader := bufio.NewReader(f)
content, _ := ioutil.ReadAll(reader)
// File content.
fmt.Println(string(content))
}
Output
This is an example file.
With two lines.
Contents of file.txt:
This is an example file.
With two lines.
File exists. Does a file or directory exist? We test 2 paths that likely point to no files or directories. We invoke os.Stat on the path strings, and store the error in "err."
Then: We pass the error variable to os.IsNotExist. We test the result in an if-statement.
IfTip: The os.IsNotExist func will work on both files and directories. If no error is returned, it will return false.
Golang program that uses Stat, os.IsNotExist
package main
import (
"fmt"
"os"
)
func main() {
directory := "/home/none/lost"
_, err := os.Stat(directory)
// See if directory exists.
// ... Use the IsNotExist method.
if os.IsNotExist(err) {
fmt.Println("Directory does not exist")
}
file := "/home/none/program.go"
_, err = os.Stat(file)
// See if the file exists.
if os.IsNotExist(err) {
fmt.Println("File does not exist")
}
}
Output
Directory does not exist
File does not exist
Get file size. Sometimes we wish to get the size in bytes of a file. We can call os.Stat and then the Size() method. This returns the count of bytes in the file.
Golang program that uses Stat and Size
package main
import (
"fmt"
"os"
)
func main() {
file := "C:\\programs\\program.go"
// Call Stat on a path string to get statistics.
stat, _ := os.Stat(file)
// Get file size.
size := stat.Size()
fmt.Println("FILE SIZE IN BYTES:", size)
}
Output
FILE SIZE IN BYTES: 310
Move file. How can we rename or move a file? We must import the "os" package, which contains many helpful file-system funcs. In main(), we have "before" and "after" locations.
Tip: We want to move the file from the before, to the after, location. We pass the 2 arguments to os.Rename.
Result: We find (by examining the files in the file manager) that the file has been moved (renamed).
Golang program that uses os.Rename, moves a file
package main
import (
"fmt"
"os"
)
func main() {
before := "/home/sam/test.txt"
after := "/home/sam/optimized.txt"
// Rename or move file from one location to another.
os.Rename(before, after)
fmt.Println("DONE")
}
Output
DONE
Write file. With ioutil, we can call WriteFile to write a byte slice to a file. For a text file, we can also use NewWriter and WriteString.
WriteFile
Import os. In the "os" package we find many helpful methods. With the os.Remove() func we can delete a file. With os.Open we can invoke Readdir to get all file names in a directory.
os.RemoveReaddir
Compression. With the "compress" package we can compress byte data and strings to gzip files. We use the NewWriter method to begin writing compressed data.
compressCSV files. With the "encoding/csv" package we can parse fields in a CSV file without using Split() calls. This makes reading comma-separated values easier.
csvIn file handling, errors are common and often unavoidable. We use the error return value from os.Open, and the recover() method, to handle these events.
Related Links:
- Golang strconv, Convert Int to String
- Golang Odd and Even Numbers
- Golang Recover Built In: Handle Errors, Panics
- Learn Go Language Tutorial
- Golang html template Example
- Golang http.Get Examples: Download Web Pages
- Golang container list Example (Linked List)
- Golang base64 Encoding Example: EncodeToString
- Golang os exec Examples: Command Start and Run
- Golang String Between, Before and After
- Golang os.Remove: Delete All Files in Directory
- Golang First Words in String
- Golang flag Examples
- Golang Regexp Find Examples: FindAllString
- Golang Regexp Examples: MatchString, MustCompile
- Golang Index, LastIndex: strings Funcs
- Golang Compress GZIP Examples
- Golang Interface Example
- Golang 2D Slices and Arrays
- Golang Sscan, Sscanf Examples (fmt)
- Top 41 Go Programming (Golang) Interview Questions (2021)
- Golang Padding String Example (Right or Left Align)
- Golang Equal String, EqualFold (If Strings Are the Same)
- Golang map Examples
- Golang Map With String Slice Values
- Golang Array Examples
- Golang Remove Duplicates From Slice
- Golang If, Else Statements
- Golang ParseInt Examples: Convert String to Int
- Golang Strings
- Golang strings.Map func
- Golang bufio.ScanBytes, NewScanner (Read Bytes in File)
- Golang Built In Functions
- Golang bytes.Buffer Examples (WriteString, Fprintf)
- Golang Bytes: Slices and Methods
- Golang Caesar Cipher Method
- Golang Chan: Channels, Make Examples
- Golang Math Module: math.Abs, Pow
- Golang Reverse String
- Golang Struct Examples: Types and Pointers
- Golang path and filepath Examples (Base, Dir)
- Golang Substring Examples (Rune Slices)
- Golang Suffixarray Examples: New, Lookup Benchmark
- Golang switch Examples
- Golang Convert Map to Slice
- Golang Convert Slice to String: int, string Slices
- Golang Const, Var Examples: Iota
- Golang ROT13 Method
- Golang strings.Contains and ContainsAny
- Golang rand, crypto: Random Number Generators
- Golang String Literal Examples (Repeat Method)
- Golang ToLower, ToUpper String Examples
- Golang Trim, TrimSpace and TrimFunc Examples
- Golang Join Examples (strings.Join)
- Golang Len (String Length)
- Golang Convert String to Rune Slice (append)
- Golang JSON Example: Marshal, Unmarshal
- Golang Replace String Examples: Replacer, NewReplacer
- Golang nil (Cannot Use nil as Type)
- Golang Slice Examples
- Golang ListenAndServe Examples (HandleFunc)
- Golang Fibonacci Sequence Example
- Golang Time: Now, Parse and Duration
- Golang bits, OnesCount (Get Bitcount From Int)
- Golang Fprint, Fprintf and Fprintln Examples (fmt)
- Golang Func Examples
- Golang csv Examples
- Golang Fields and FieldsFunc
- Golang unicode.IsSpace (If Char Is Whitespace)
- Golang fmt.Println Examples
- Golang for Loop Examples: Foreach and While
- Golang ioutil.WriteFile, os.Create (Write File to Disk)
- Golang File Handling
- Golang range: Slice, String and Map
- Golang Readdir Example (Get All Files in Directory)
- Golang Sort Slice: Len, Less, Swap in Interface
- Golang Get Lines in File (String Slice)
- Golang Split Examples (SplitAfter, SplitN)
Related Links
Adjectives
Ado
Ai
Android
Angular
Antonyms
Apache
Articles
Asp
Autocad
Automata
Aws
Azure
Basic
Binary
Bitcoin
Blockchain
C
Cassandra
Change
Coa
Computer
Control
Cpp
Create
Creating
C-Sharp
Cyber
Daa
Data
Dbms
Deletion
Devops
Difference
Discrete
Es6
Ethical
Examples
Features
Firebase
Flutter
Fs
Git
Go
Hbase
History
Hive
Hiveql
How
Html
Idioms
Insertion
Installing
Ios
Java
Joomla
Js
Kafka
Kali
Laravel
Logical
Machine
Matlab
Matrix
Mongodb
Mysql
One
Opencv
Oracle
Ordering
Os
Pandas
Php
Pig
Pl
Postgresql
Powershell
Prepositions
Program
Python
React
Ruby
Scala
Selecting
Selenium
Sentence
Seo
Sharepoint
Software
Spellings
Spotting
Spring
Sql
Sqlite
Sqoop
Svn
Swift
Synonyms
Talend
Testng
Types
Uml
Unity
Vbnet
Verbal
Webdriver
What
Wpf