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
+18.9k Nginx + FastCGI + Go Setup.
+4.9k Golang : Launching your executable inside a console under Linux
+4.4k Golang : Detect words using using consecutive letters in a given string
+10k How to test Facebook App on localhost ?
+15.4k CodeIgniter/PHP : Create directory if does not exist example
+3.1k Golang : PGX CopyFrom to insert rows into Postgres database
+8.6k Golang : Eroding and dilating image with OpenCV example
+10.7k Golang : Clean formatting/indenting or pretty print JSON result
+7.7k Golang : Sort lines of text example
+6.6k Golang : What fmt.Println() can do and println() cannot do
+12.2k Golang : Read XML elements data with xml.CharData example
+28.4k Golang : How to verify uploaded file is image or allowed file types