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
+13.7k Golang : Get HTTP protocol version example
+32.5k Golang : Call a function after some delay(time.Sleep and Tick)
+12.7k Golang : Get current time
+7.9k Golang : How to get username from email address
+6k Golang : How to iterate a slice without using for loop?
+6.9k Golang : Handle Palindrome string with case sensitivity and unicode
+9k Golang : Use regular expression to get all upper case or lower case characters example
+6.2k Golang : Gorrila mux.Vars() function example
+6.9k Golang : Configure Apache and NGINX to access your Go service example
+13.3k Golang : How to shuffle elements in array or slice?
+16.3k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+22.8k Golang : How to validate URL the right way