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
+3.7k Chrome : How to block socketloop.com links in Google SERP?
+45.5k Golang : Convert int to byte array([]byte)
+4.4k Golang : If else example and common mistake
+6.5k Golang : Convert source code to assembly language
+18.5k Golang : Append content to a file
+16k Golang : How to save log messages to file?
+16.2k Golang : Parse date string and convert to dd-mm-yyyy format
+7.8k Golang : Get curl -I or head data from URL example
+23.7k Golang : Storing cookies in http.CookieJar example
+19.9k Golang : How to get time zone and load different time zone?
+7k Golang : HttpRouter multiplexer routing example