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
+6.2k Golang : Dealing with backquote
+11.1k How to test Facebook App on localhost ?
+10.4k Golang : How to check if a website is served via HTTPS
+11.6k Get form post value in Go
+10.2k Golang : Check a web page existence with HEAD request example
+12.6k Golang : Forwarding a local port to a remote server example
+14.5k Golang : On enumeration
+10k Golang : Check if user agent is a robot or crawler example
+30.5k Golang : How to verify uploaded file is image or allowed file types
+8.9k Golang : Sort lines of text example
+12.3k Golang : calculate elapsed run time
+13k Python : Convert IPv6 address to decimal and back to IPv6