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
+18k Golang : Read binary file into memory
+11k Golang : Characters limiter example
+11.6k Golang : Detect user location with HTML5 geo-location
+8.1k Golang : Generate Datamatrix barcode
+5.2k Clean up Visual Studio For Mac installation failed disk full problem
+4.4k JavaScript : Rounding number to decimal formats to display currency
+7.2k Golang : Detect sample rate, channels or latency with PortAudio
+4.4k JavaScript: Add marker function on Google Map
+20.3k Golang : Convert date string to variants of time.Time type examples
+4.8k Golang : Experimental Jawi programming language
+19.5k Golang : How to run your code only once with sync.Once object
+8k Useful methods to access blocked websites