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
+5.8k Golang : Reverse by word
+20k Golang : Encrypt and decrypt data with TripleDES
+8k Golang : Serving HTTP and Websocket from different ports in a program example
+6.6k Golang : Gorrila set route name and get the current route name
+4.5k PHP : Fix Call to undefined function curl_init() error
+20.7k Golang : How to reverse slice or array elements order
+9k Golang : Edge detection with Sobel method
+10.1k Use systeminfo to find out installed Windows Hotfix(s) or updates
+12.9k Golang : How to check if your program is running in a terminal
+13.2k Golang : ROT47 (Caesar cipher by 47 characters) example
+7k Golang : How to feed or take banana with Gorilla Web Toolkit Session package