读文件
package main
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
open, err := os.Open("QQ图片20211025150227.png")
if err != nil {
fmt.Println("open err=", err)
}
defer open.Close()
bytes, err := ioutil.ReadAll(open)
if err != nil {
fmt.Println("read err=", err)
}
fmt.Println(string(bytes))
}
写文件
package main
import (
"fmt"
"io/ioutil"
"os"
)
func read() (bytes []byte) {
open, err := os.Open("QQ图片20211025150227.png")
if err != nil {
fmt.Println("open err=", err)
}
defer open.Close()
bytes, err = ioutil.ReadAll(open)
if err != nil {
fmt.Println("read err=", err)
}
return
}
func main() {
file, err := os.OpenFile("to.txt", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println("openfile err=", err)
}
n, err := file.Write(read())
if err != nil {
fmt.Println("write err=", err)
}
fmt.Println(n)
}