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.6k Golang : Resumable upload to Google Drive(RESTful) example
+81.4k Golang : How to convert character to ASCII and back
+26.8k Golang : Connect to database (MySQL/MariaDB) server
+6.1k Golang : Squaring elements in array
+7.1k Golang : Reverse text lines or flip line order example
+17.2k Golang : Read binary file into memory
+17.7k Golang : Calculate entire request body length during run time
+9.1k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+7.7k Golang : Find network service name from given port and protocol
+6.4k Golang : Not able to grep log.Println() output
+18.8k Golang : Read directory content with os.Open
+7.2k Golang : Check if integer is power of four example