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
+7.4k Golang : Example of custom handler for Gorilla's Path usage.
+39.7k Golang : Remove dashes(or any character) from string
+7.3k Golang : Not able to grep log.Println() output
+5.6k PHP : Convert CSV to JSON with YQL example
+8.1k Golang : Handle Palindrome string with case sensitivity and unicode
+13.6k Golang : Read XML elements data with xml.CharData example
+16k Golang : Get file permission
+25.9k Golang : Daemonizing a simple web server process example
+9.1k Golang : Capture text return from exec function example
+21.7k Golang : GORM create record or insert new record into database example
+10k Golang : Get escape characters \u form from unicode characters
+11.4k Golang : Fix fmt.Scanf() on Windows will scan input twice problem