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
+12.3k Golang : Reverse IP address for reverse DNS lookup example
+12.4k Golang : Human readable time elapsed format such as 5 days ago
+5.7k Golang : Output or print out JSON stream/encoded data
+4.4k Swift : Convert string array to array example
+4.9k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+4.6k Golang : Grab news article text and use NLP to get each paragraph's sentences
+5.7k Golang : How to fix html/template : "somefile" is undefined error?
+14.2k Golang : Loop each day of the current month example
+8.4k Golang : interface - when and where to use examples
+6.4k Golang : Convert(cast) io.Reader type to string
+4.8k Golang : Launching your executable inside a console under Linux
+10.6k Golang : Pagination with go-paginator configuration example