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
+13.9k Golang : Gin framework accept query string by post request example
+6.1k Golang : Missing Subversion command
+6.9k Golang : Join lines with certain suffix symbol example
+11.6k Golang : Generate DSA private, public key and PEM files example
+6.9k Golang : Pat multiplexer routing example
+16.5k Golang : Send email and SMTP configuration example
+14.4k Golang : How to shuffle elements in array or slice?
+10.2k Golang : Find and replace data in all files recursively
+20.8k Android Studio : AlertDialog and EditText to get user string input example
+6.8k Golang : Check if password length meet the requirement
+7k Golang : How to setup a disk space used monitoring service with Telegram bot
+6.8k Golang : Reverse by word