Home
Go
File Handling
Updated Jan 27, 2025
Dot Net Perls
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, and the classes are easy to use.
log
Scan 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.
Start Open() returns a *File descriptor. We can pass the result of Open() to the bufio.NewScanner method.
Next NewScanner() creates a new *Scanner. The File we pass to this method is accessed through its Reader interface.
Finally Scan() 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.
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) } }
Carrot Bird Fish Turnip
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().
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) } }
a commodius vicus of recirculation
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.
Note We use bufio.NewReader to create a buffered text file reader. We can then pass this to ReadAll.
Note 2 The ReadAll func returns a byte slice. We can convert the byte slice into a string—this makes it easier to display.
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)) }
This is an example file. With two lines.
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.
if
Tip The os.IsNotExist func will work on both files and directories. If no error is returned, it will return false.
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") } }
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.
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) }
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).
Copy 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") }
DONE
Write file. For a text file, we can also use NewWriter and WriteString. In this example we first invoke os.Create, and then call bufio.NewWriter.
Tip We can write text to a file with the WriteString function. We then Flush the file to ensure it is written.
Info It is possible to write byte data to a file as well. Other packages, like ioutil, are often helpful.
ioutil.WriteFile
package main import ( "bufio" "os" "fmt" ) func main() { // Get file writer set up. file, _ := os.Create(`C:\programs\hello.txt`) w := bufio.NewWriter(file) // Write to file. w.WriteString("Hello friend") w.Flush() fmt.Println("DONE") }
DONE
ReadDir. In the "os" package we find many helpful methods. With os.Open we can invoke Readdir to get all file names in a directory.
ReadDir
Tip With Getwd(), we get the current working directory. Then we can use os.Open and ReadDir to get the files.
package main import ( "fmt" "os" ) func main() { // Open the working directory and call ReadDir. d, _ := os.Getwd() dir, _ := os.Open(d) files, _ := dir.ReadDir(0) // Loop over files and print their names. for _, f := range files { fmt.Println(f.Name()) } }
program.go example.txt
In 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.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jan 27, 2025 (new example).
Home
Changes
© 2007-2025 Sam Allen