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
+20.1k Golang : Convert(cast) string to rune and back to string example
+5.8k Golang : Pat multiplexer routing example
+14.9k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+8.2k Golang : Terminate-stay-resident or daemonize your program?
+22.8k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+14.1k Golang : Get query string value on a POST request
+12.7k Golang : convert rune to unicode hexadecimal value and back to rune character
+13.6k Golang : Missing Bazaar command
+5.7k Default cipher that OpenSSL used to encrypt a PEM file
+9.4k Golang : Select region of interest with mouse click and crop from image
+19.1k Golang : Pipe output from one os.Exec(shell command) to another command
+3.5k Golang : Valued expressions and functions example