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
+2.7k Mac OSX : Get disk partitions' size, type and name
+3.3k Golang : Return multiple values from function
+4.7k Swift : substringWithRange() function example
+4.6k Golang : Get all countries phone codes
+12.3k Golang : How do I get the local IP (non-loopback) address ?
+2.9k Cash Flow : 50 days to pay your credit card debt
+2.7k Which content-type(MIME type) to use for JSON data
+56.5k Golang : How to convert character to ASCII and back
+18.3k Mac/Linux and Golang : Fix bind: address already in use error
+9.8k Golang : Chunk split or divide a string into smaller chunk example
+7k JavaScript/JQuery : Detect or intercept enter key pressed example
+8.4k Golang : Concurrency and goroutine example