Golang : Read file with ioutil
This tutorial will show you how to read a file into buffer and display the content in Go. This example read plain text file, if you are reading a binary file... change fmt.Println(string(file))
to fmt.Println(file)
(without the string).
Reading binary file will be more tricky as you need to know the format before reading the file. It will be covered in another tutorial.
For now, this is the most basic example of reading a file in Go with io/ioutil
readfileioutil.go
package main
import (
"fmt"
"io/ioutil"
)
func main() {
file, err := ioutil.ReadFile("testfile.txt")
if err != nil {
fmt.Println(err)
return
}
// out the file content
fmt.Println(string(file))
}
See also : Golang : Read file
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+16.9k Golang : read gzipped http response
+5.2k Golang : Issue HTTP commands to server and port example
+12.9k Python : Convert IPv6 address to decimal and back to IPv6
+11.4k Golang : How to flush a channel before the end of program?
+16.3k Golang : convert string or integer to big.Int type
+9.1k Golang : Intercept and compare HTTP response code example
+11.9k Golang : Convert(cast) bigint to string
+12.2k Golang : md5 hash of a string
+15.7k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+7.7k Golang : Test if an input is an Armstrong number example
+11.5k CodeIgniter : Import Linkedin data
+8.8k Golang : Get final balance from bit coin address example