go ioutil.ReadFile 和ioutil.WriteFile
作者:互联网
ioutil.WriteFile
package main
import (
"fmt"
"io/ioutil"
)
func main(){
str := "hello world \n世界和平"
err := ioutil.WriteFile("./test.txt",[]byte(str),0755)
if err != nil {
fmt.Println("open file failed err",err)
return
}
}
ioutil.ReadFile
package main
import (
"fmt"
"io/ioutil"
)
func main(){
// ioutil封装了for{}循环,可以直接读取整个文件,返回整个文件内容到切片中
//content,err := ioutil.ReadFile("./main.go")
content,err := ioutil.ReadFile("D:\\GOPATH\\src\\MyProject\\pingguoxueyuan\\gostudy\\listen15\\ioutil\\main.go")
if err != nil {
fmt.Println("read file failed ,err: err")
return
}
fmt.Println(content) // 字节的切片[]uint8
fmt.Printf("content的类型:%T\n",content) // content的类型:[]uint8
fmt.Println(string(content)) // string 把[]uint8切片转化为 string
}
标签:ioutil,err,fmt,ReadFile,content,WriteFile,go,main 来源: https://www.cnblogs.com/heris/p/16030261.html