Golang : Write file with io.WriteString



Tags : golang write-file

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()
 }

Affiliate :

Hire Golang Developers




Tags : golang write-file

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