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 : Execute function at intervals or after some delay
+22.9k Golang : Test file read write permission example
+29.4k Golang : How to create new XML file ?
+14.3k Android Studio : Use image as AlertDialog title with custom layout example
+8.5k Android Studio : Import third-party library or package into Gradle Scripts
+27.4k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+13.9k Golang : concatenate(combine) strings
+7.9k Golang : Grayscale Image
+6.5k Golang : How to determine if request or crawl is from Google robots
+10.5k Golang : Bubble sort example
+5.4k How to check with curl if my website or the asset is gzipped ?
+8k Golang : Multiplexer with net/http and map