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.9k Golang : Array mapping with Interface
+25.7k Mac/Linux and Golang : Fix bind: address already in use error
+4.8k Linux : How to set root password in Linux Mint
+7k Golang : Dealing with postal or zip code example
+11.7k Golang : Find and draw contours with OpenCV example
+32k Golang : Copy directory - including sub-directories and files
+5.3k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+12.3k Golang : Add ASCII art to command line application launching process
+16k Golang : convert string or integer to big.Int type
+21.1k Golang : How to read float value from standard input ?
+9.2k Facebook : Getting the friends list with PHP return JSON format
+23.6k Golang : Use regular expression to validate domain name