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
+9.1k Golang : Simple histogram example
+9.9k Golang : Translate language with language package example
+51.1k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+31.9k Golang : Convert an image file to []byte
+17.6k Golang : delete and modify XML file content
+9.8k Golang : Qt get screen resolution and display on center example
+22.8k Golang : untar or extract tar ball archive example
+6.3k Golang : Extract sub-strings
+19.4k Golang : Fix cannot download, $GOPATH not set error
+6.3k Golang : Detect face in uploaded photo like GPlus
+6k PHP : How to check if an array is empty ?
+5k Golang : Calculate a pip value and distance to target profit example