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.4k Golang : Split strings into command line arguments
+3.9k Linux/MacOSX : Search for files by filename and extension with find command
+3.5k Golang : Generate Interleaved 2 inch by 5 inch barcode
+5.5k Golang : Multiplexer with net/http and map
+4k Fix fatal error: evacuation not done in time problem
+4.8k Golang : Get expvar(export variables) to work with multiplexer
+17.3k Golang : For loop continue,break and range
+8.4k Golang : Verify Linux user password again before executing a program example
+10.9k Golang : GUI with Qt and OpenCV to capture image from camera
+10.8k Golang : Check if an integer is negative or positive
+3.2k Golang : Detect words using using consecutive letters in a given string
+24.9k Golang : JQuery AJAX post data to server and send data back to client example