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
+36.1k Golang : Convert date or time stamp from string to time.Time type
+10.9k Golang : Intercept and process UNIX signals example
+9.9k Golang : Print how to use flag for your application example
+18k Golang : Get path name to current directory or folder
+21.8k Golang : Securing password with salt
+17.2k Golang : Find smallest number in array
+8k Prevent Write failed: Broken pipe problem during ssh session with screen command
+8.5k Golang : Gorilla web tool kit schema example
+8.8k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+10k Golang : cannot assign type int to value (type uint8) in range error
+52k Golang : How to get struct field and value by name
+4.8k Linux : How to set root password in Linux Mint