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
+8.7k Golang : Combine slices but preserve order example
+16.7k Golang : Gzip file example
+11.2k CodeIgniter : How to check if a session exist in PHP?
+14.9k Golang : Submit web forms without browser by http.PostForm example
+11.9k Golang : Convert decimal number(integer) to IPv4 address
+18.5k Golang : Aligning strings to right, left and center with fill example
+23k Golang : Calculate time different
+13.9k Golang : Get dimension(width and height) of image file
+8.3k Golang : Implementing class(object-oriented programming style)
+10.6k Golang : Flip coin example
+14.4k Golang : On enumeration
+17.9k Golang : Qt image viewer example