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
+12.5k Golang : Check if an integer is negative or positive
+9.6k Golang : Replace a parameter's value inside a configuration file example
+5.1k Golang : Extract XML attribute data with attr field tag example
+8.2k Golang : Extract or copy items from map based on value
+5.8k Golang : Get expvar(export variables) to work with multiplexer
+21.1k Golang : Read a file into an array or slice example
+4.8k Golang : Error handling methods
+23.7k Golang : Convert long hexadecimal with strconv.ParseUint example
+8k Golang : Generate random Chinese, Japanese, Korean and other runes
+17k Golang : Iterating Elements Over A List
+3.9k Unix/Linux : How to pipe/save output of a command to file?