1,操作文件
1.1,文件流的介绍
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5a9fe43759b8f2bf102ffb3946e646a3.png)
1.2,打开文件和关闭文件
package main
import (
"fmt"
"os"
)
func main() {
file,err := os.Open("E:/go.txt")
if(err!=nil){
fmt.Println("open file err:",err)
}
fmt.Printf("file=%v",&file)
file.Close()
}
1.3,代缓冲带的reader读文件
package main
import (
"bufio"
"fmt"
"io"
"os"
)
func main() {
file,err := os.Open("E:/go.txt")
if(err!=nil){
fmt.Println("open file err:",err)
}
defer file.Close()
reader :=bufio.NewReader(file)
for{
str,err2:=reader.ReadString('\n')
fmt.Println(str)
if(err2 == io.EOF){
break
}
}
fmt.Println("文件读取完毕")
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/955b291db9d7d7a096fa6ca9494739b5.png)
1.4,一次性读取文件
package main
import (
"fmt"
"io/ioutil"
)
func main() {
file := "E:/go.txt"
content,err := ioutil.ReadFile(file)
if(err!=nil){
fmt.Println("open file err:",err)
}
fmt.Printf("%v",string(content))
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/345f487bbf59f8801e1f89f642df742a.png)
1.5,写文件操作介绍
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c87108bc67f4e4e9ed71c3445dd61099.png)
1.6,文件打开模式介绍
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b395448205a260e75f2ac58b7bf09c53.png)
1.7,写文件案例
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
filePath:="E:/go2.txt"
file,err := os.OpenFile(filePath,os.O_WRONLY|os.O_CREATE|os.O_APPEND|os.O_TRUNC,0666)
defer file.Close()
if(err!=nil){
fmt.Println("open file err:",err)
}
str := "hello file\n"
writer := bufio.NewWriter(file)
for i:=0;i<5 ;i++ {
writer.WriteString(str)
}
writer.Flush()
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/48dc7aec99975ac6fd96004d45ed6cce.png)
1.8,拷贝文件并判断文件或者目录是否存在
package main
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
filePath01:="E:/go2.txt"
filePath02:="E:/go3.txt"
data,err := ioutil.ReadFile(filePath01)
if(err!=nil){
fmt.Println("read file err %v",err)
}
if(os.IsNotExist(err)){
fmt.Printf(filePath01+"目录不存在")
}
err =ioutil.WriteFile(filePath02,data,0666)
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3a64958c4e2e91fb09799b230c47cbb3.png)
1.9,拷贝图片
package main
import (
"bufio"
"fmt"
"io"
"os"
)
func CopyFile(dstFileName string,srcFileName string)(written int64,err error){
srcFile,err := os.Open(srcFileName)
if(err!=nil){
fmt.Println("OPEN srcFile ERR= %v\n",err)
}
reader := bufio.NewReader(srcFile)
dstFile,err2 := os.OpenFile(dstFileName,os.O_WRONLY|os.O_CREATE|os.O_APPEND|os.O_TRUNC,0666)
if(err2 != nil){
fmt.Println("OPEN dstFile ERR= %v\n",err2)
}
writer:= bufio.NewWriter(dstFile)
return io.Copy(writer,reader)
}
func main() {
CopyFile("E:/2.png","E:/1.png")
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/efa21d87985340748b9f98a66042ee6b.png)
2,JOSN
2.1,结构体,map,切片序列化
package main
import (
"encoding/json"
"fmt"
)
type Monster struct {
Name string
Age int
Birthday string
Sal float64
Skill string
}
func testStruct() {
monster:=Monster{
Name: "牛魔王",
Age: 500,
Birthday: "2011-11-11",
Sal: 8000.0,
Skill: "牛魔拳",
}
content,err := json.Marshal(monster)
if(err!=nil){
fmt.Println("json err:",err)
}
fmt.Println("json struct:",string(content))
}
func testMap() {
var a3 map[string]string = map[string]string{
"大姐姐":"林黛玉",
"二姐姐":"薛宝钗",
"三姐姐":"王熙凤",
}
content,err := json.Marshal(a3)
if(err!=nil){
fmt.Println("json err:",err)
}
fmt.Println("json Map:",string(content))
}
func testSlice() {
var a3 map[string]interface{}
a3 = make(map[string]interface{})
a3["key1"]="lnl1"
a3["key2"]="lnl2"
a3["key3"]="lnl3"
var slice[]map[string]interface{}
slice = append(slice,a3)
content,err := json.Marshal(slice)
if(err!=nil){
fmt.Println("json err:",err)
}
fmt.Println("json Slice:",string(content))
}
func main() {
testStruct()
testMap()
testSlice()
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2b54aa7f6d57d0270c8b00d1046bbe90.png)
2.2,struct序列化时tag的使用
package main
import (
"encoding/json"
"fmt"
)
type Monster2 struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday string `json:"birthday"`
Sal float64 `json:"sal"`
Skill string `json:"skill"`
}
func testStruct2() {
monster:=Monster2{
Name: "牛魔王",
Age: 500,
Birthday: "2011-11-11",
Sal: 8000.0,
Skill: "牛魔拳",
}
content,err := json.Marshal(monster)
if(err!=nil){
fmt.Println("json err:",err)
}
fmt.Println("json struct:",string(content))
}
func main() {
testStruct2()
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/caefb841ba24ef05e1db39051fb27848.png)
2.3,结构体,map,切片反序列化
package main
import (
"encoding/json"
"fmt"
)
type Monster3 struct {
Name string
Age int
Birthday string
Sal float64
Skill string
}
func jsonToStruct() {
str:="{\"Name\":\"牛魔王\",\"Age\":500,\"Birthday\":\"2011-11-11\",\"Sal\":8000,\"Skill\":\"牛魔拳\"}"
var monster Monster3
err := json.Unmarshal([]byte(str),&monster)
if(err!=nil){
fmt.Println("jsonToStruct err:",err)
}
fmt.Println(monster.Name)
fmt.Println(monster.Age)
fmt.Println(monster.Birthday)
fmt.Println(monster.Sal)
fmt.Println(monster.Skill)
}
func jsonToMap() {
str:="{\"三姐姐\":\"王熙凤\",\"二姐姐\":\"薛宝钗\",\"大姐姐\":\"林黛玉\"}"
var a map[string]interface{}
err := json.Unmarshal([]byte(str),&a)
if(err!=nil){
fmt.Println("jsonToStruct err:",err)
}
for key, value := range a {
fmt.Println("key=>",key)
fmt.Println("value=>",value)
}
}
func jsonToSlice() {
str:="[{\"key1\":\"lnl1\",\"key2\":\"lnl2\",\"key3\":\"lnl3\"}]"
var slice[]map[string]interface{}
err := json.Unmarshal([]byte(str),&slice)
if(err!=nil){
fmt.Println("jsonToStruct err:",err)
}
for i := range slice {
data:=slice[i]
fmt.Println(data)
}
}
func main() {
jsonToStruct()
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5df5d0a90fe810f7e9306b0a03f29421.png)