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
+14.5k Golang : Simple word wrap or line breaking example
+6.6k Golang : Map within a map example
+18.5k Golang : How to get hour, minute, second from time?
+26.6k Golang : Get executable name behind process ID example
+5.9k Golang : Launching your executable inside a console under Linux
+19.3k Golang : Check whether a network interface is up on your machine
+6k AWS S3 : Prevent Hotlinking policy
+9.1k Golang : How to use Gorilla webtoolkit context package properly
+24.7k Golang : GORM read from database example
+6.8k Golang : Humanize and Titleize functions
+5.5k Golang : fmt.Println prints out empty data from struct
+10.3k Golang : Check a web page existence with HEAD request example