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
+16.9k Golang : How to generate QR codes?
+12.3k Golang : Simple client-server HMAC authentication without SSL example
+10k Golang : Channels and buffered channels examples
+9.5k Golang : Apply Histogram Equalization to color images
+7.2k Golang : Use modern ciphers only in secure connection
+14.5k Golang : Find network of an IP address
+9.8k Golang : Qt get screen resolution and display on center example
+6.4k CodeIgniter : form input set_value cause " to become & quot
+8k Golang : What fmt.Println() can do and println() cannot do
+11.2k Golang : Simple image viewer with Go-GTK
+22k Golang : Use TLS version 1.2 and enforce server security configuration over client
+30.5k Golang : Generate random string