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
+11.2k Golang : How to check if a string starts or ends with certain characters or words?
+5.8k Golang : How to fix html/template : "somefile" is undefined error?
+16.3k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+5.1k Golang : Experimenting with the Rejang script
+14.8k Golang : Delete files by extension
+4.2k Python : Create Whois client or function example
+7.4k PHP : How to parse ElasticSearch JSON ?
+8k Golang : Go as a script or running go with shebang/hashbang style
+7.7k Golang : Sort lines of text example
+8.8k PHP : Get coordinates latitude/longitude from string
+6.3k Golang : Rot13 and Rot5 algorithms example
+9.1k Golang : Text file editor (accept input from screen and save to file)