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
+5.6k Golang : Stop goroutine without channel
+25.2k Golang : Create PDF file from HTML file
+36.6k Golang : How to split or chunking a file to smaller pieces?
+12.2k Golang : Clean formatting/indenting or pretty print JSON result
+8.8k Golang : Set or add headers for many or different handlers
+22k Golang : Convert string slice to struct and access with reflect example
+23.9k Find and replace a character in a string in Go
+5.3k JavaScript/JQuery : Redirect page examples
+7.6k Golang : Detect sample rate, channels or latency with PortAudio
+10.8k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+8k Golang : Example of how to detect which type of script a word belongs to
+11.3k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example