Go language to write a string to a file

  • 2020-05-19 05:00:29
  • OfStack

This example demonstrates how the Go language writes strings to files. Share with you for your reference. The specific implementation method is as follows:

package  main
import "fmt"
import "os"
func main() {
    fileName := "test.dat"
    dstFile,err := os.Create(fileName)
    if err!=nil{
        fmt.Println(err.Error())  
        return
    } 
    defer dstFile.Close()
    s:="hello world"
    dstFile.WriteString(s + "\n")
}

I hope this article has been helpful to your programming of Go language.


Related articles: