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
+2.9k Golang : Find network service name from given port and protocol
+2.8k Golang : Variadic function arguments sanity check example
+18.5k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions
+23.5k Golang : Upload and download file to/from AWS S3
+12.9k Golang : Pipe output from one os.Exec(shell command) to another command
+2.3k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+3.8k Golang : Gorilla web tool kit schema example
+4.8k Golang : Intercept and process UNIX signals example
+1.1k Golang : Calculate a pip value and distance to target profit example
+14.9k Golang : convert int to string
+15.5k Golang : Storing cookies in http.CookieJar example
+32.1k Golang : How to read CSV file