Golang io/ioutil.WriteFile() function example
package io/ioutil
Golang io/ioutil.WriteFile() function usage example
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
)
func main() {
fileName := "./file.dat"
str := []byte("世界你好! Hello World!")
ioutil.WriteFile(fileName, str, os.ModeAppend)
// somehow os.ModeAppend did not set ANY mode to the file.dat
// and this will prevent ioutil.ReadFile from reading the
// file.dat content
// therefore, we need to change the mode manually
cmd := exec.Command("chmod", "666", "file.dat")
out, err := cmd.Output()
if err != nil {
fmt.Println(err)
}
fmt.Printf(string(out))
readerFile, _ := ioutil.ReadFile(fileName)
fmt.Printf("%s \n", readerFile)
}
Output :
世界你好! Hello World!
Reference :
Advertisement
Something interesting
Tutorials
+27.3k PHP : Count number of JSON items/objects
+30.6k Golang : Calculate percentage change of two values
+11.1k Golang : Post data with url.Values{}
+9k Golang : Generate random Chinese, Japanese, Korean and other runes
+7.4k Gogland : Where to put source code files in package directory for rookie
+4.8k Python : Convert(cast) bytes to string example
+5.1k Golang : Return multiple values from function
+9.9k Golang : Text file editor (accept input from screen and save to file)
+10.9k Golang : How to pipe input data to executing child process?
+14.2k Golang : Reset buffer example
+12.4k Golang : Pass database connection to function called from another package and HTTP Handler
+6.7k Golang : Gargish-English language translator