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
+7k Golang : How to iterate a slice without using for loop?
+7.6k Golang : Getting Echo framework StartAutoTLS to work
+9.3k Golang : Extract or copy items from map based on value
+8.2k Golang : How to check variable or object type during runtime?
+14.1k Android Studio : Use image as AlertDialog title with custom layout example
+6.8k Golang : How to call function inside template with template.FuncMap
+21.4k SSL : How to check if current certificate is sha1 or sha2
+6.6k Golang : Muxing with Martini example
+18.9k Golang : Calculate entire request body length during run time
+20.2k Android Studio : AlertDialog and EditText to get user string input example
+3.1k Golang : Fix go-cron set time not working issue