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.2k Golang : Array mapping with Interface
+12.6k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+7.3k CloudFlare : Another way to get visitor's real IP address
+22.4k Golang : Securing password with salt
+9.5k Golang : Web(Javascript) to server-side websocket example
+7.9k Golang : Generate human readable password
+7.2k Golang : Transform lisp or spinal case to Pascal case example
+14.7k Golang : Execute function at intervals or after some delay
+9.1k Golang : automatically figure out array length(size) with three dots
+20.4k Golang : Reset or rewind io.Reader or io.Writer
+21.9k SSL : How to check if current certificate is sha1 or sha2
+29.2k Golang : Get first few and last few characters from string