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
+18.7k Golang : Delete duplicate items from a slice/array
+11.9k Golang : Sort and reverse sort a slice of runes
+9.6k Golang : Detect number of active displays and the display's resolution
+4.8k Golang : Calculate a pip value and distance to target profit example
+33.8k Golang : Proper way to set function argument default value
+12.8k Golang : Get terminal width and height example
+10.7k Golang : Get UDP client IP address and differentiate clients by port number
+50.7k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+5.9k PageSpeed : Clear or flush cache on web server
+10k Golang : How to tokenize source code with text/scanner package?
+9.9k Golang : Read file and convert content to string
+6.6k Golang : Experimental emojis or emoticons icons programming language