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
+8.8k Golang : Bcrypting password
+8.9k Golang : Translate language with language package example
+28.7k Golang : How to get HTTP request header information?
+4.5k Python : Delay with time.sleep() function example
+8.8k Golang : Identifying Golang HTTP client request
+9.6k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example
+8.8k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+19.7k Golang : Create and resolve(read) symbolic links
+20k Golang : How to get time zone and load different time zone?
+29.8k Golang : Get local IP and MAC address
+20.7k Golang : Print leading(padding) zero or spaces in fmt.Printf?