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
+21.9k Golang : Repeat a character by multiple of x factor
+7.7k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+51.1k Golang : Check if item is in slice/array
+29k Golang : missing Git command
+4.5k Chrome : How to block socketloop.com links in Google SERP?
+35k Golang : Strip slashes from string example
+7.9k Golang : Variadic function arguments sanity check example
+11.3k Golang : Display a text file line by line with line number example
+14.3k Golang : How to get URL port?
+6.8k Golang : Find the shortest line of text example
+9.5k Golang : interface - when and where to use examples
+12.4k Golang : Transform comma separated string to slice example