Golang : Write file with io.WriteString
Writing to a file in Golang is easy. There are couple of ways to do it. In this tutorial, we will show you how to write into plain text file.
package main
import (
"os"
"io"
"fmt"
)
func main() {
filename := "output.txt"
file, err := os.Create(filename)
if err != nil {
fmt.Println(err)
}
fmt.Println(" Write to file : " + filename)
n, err := io.WriteString(file, " Hello World !")
if err != nil {
fmt.Println(n, err)
}
file.Close()
}
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
+14.8k How to automatically restart your crashed Golang server
+22.3k Golang : Repeat a character by multiple of x factor
+9.8k Golang : Read file with ioutil
+16.9k Golang : Get own process identifier
+33.9k Golang : All update packages with go get command
+31.1k Golang : Interpolating or substituting variables in string examples
+18.8k Golang : Get download file size
+13.3k Golang : Handle or parse date string with Z suffix(RFC3339) example
+13.4k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+10.3k Golang : Identifying Golang HTTP client request
+12.5k Golang : 2 dimensional array example
+7.5k Golang : How to iterate a slice without using for loop?