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
+4.6k Unix/Linux : How to pipe/save output of a command to file?
+14.7k Golang : Submit web forms without browser by http.PostForm example
+40.9k Golang : How to count duplicate items in slice/array?
+9.5k Golang : Sort and reverse sort a slice of floats
+19k Golang : Check if directory exist and create if does not exist
+8.1k Golang : Auto-generate reply email with text/template package
+22.3k Golang : How to read JPG(JPEG), GIF and PNG files ?
+8.9k Golang : How to use Gorilla webtoolkit context package properly
+27.8k Golang : Move file to another directory
+16.7k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+7.1k Golang : Check if one string(rune) is permutation of another string(rune)
+4.9k Golang : Constant and variable names in native language