直接上代码示例
package main import ( "bytes" "encoding/binary" "encoding/gob" "encoding/json" "encoding/xml" "fmt" "os" ) type Website struct { Name string `xml:"name,attr"` Url string Course []string } //json 解析 func EncodeJson() { info :=[]Website{ {"Golang","https://c.biancheng.net",[]string{"https://c.biancheng.net/cplus","http://c.biancheng.net/linux"}}, {"Java","http://c.biancheng.net/java",[]string{"https://c.biancheng.net/java1","http://c.biancheng.net/java2"}}, } //创建文件 filePtr ,err := os.Create("info.json") if err !=nil{ fmt.Println("create file failed err: ",err) return } defer filePtr.Close() //创建json编码器 encoder := json.NewEncoder(filePtr) err = encoder.Encode(info) if err !=nil{ fmt.Println("encode failed err:",err) return }else{ fmt.Println("encode success ...") } } func DecodeJson() { filePtr,err := os.Open("info.json") if err !=nil{ fmt.Println("open file failed err :",err) return } defer filePtr.Close() var info []Website decoder := json.NewDecoder(filePtr) err = decoder.Decode(&info) if err !=nil{ fmt.Println("decoder file failed err :",err) return }else{ fmt.Println("decoder file success...") fmt.Println(info) } } //xml func EncodeXml(){ info := []Website{ {"Perl","http://perl.org.cn",[]string{"perl语言入门","Golang 语言入门"}}, } file ,err := os.Create("info.xml") if err !=nil{ fmt.Println("create file failed err :",err) return } defer file.Close() encoder := xml.NewEncoder(file) err = encoder.Encode(info) if err !=nil{ fmt.Println("encode info failed err :",err) return }else{ fmt.Println("encode info to file success...") } } func DecodeXml() { file,err := os.Open("info.xml") if err !=nil{ fmt.Println("open file failed err :",err) return } defer file.Close() var info []Website decoder := xml.NewDecoder(file) err = decoder.Decode(&info) if err !=nil{ fmt.Println("decode file failed err :",err) return }else{ fmt.Println("decode file success...") fmt.Println(info) } } func EncodeGob() { info := map[string]string{ "name":"test", "website":"https://web.com", } file,_ := os.OpenFile("demo.gob",os.O_CREATE|os.O_RDWR,0777) defer file.Close() encoder := gob.NewEncoder(file) if err :=encoder.Encode(info); err !=nil{ fmt.Println(err) } } func DecodeGob() { m1 := make(map[string]string) file,_ := os.Open("demo.gob") defer file.Close() decoder := gob.NewDecoder(file) if err := decoder.Decode(&m1);err !=nil{ fmt.Println("decode failed err :",err) return } fmt.Println(m1) } //自定义二进制文件 type WebInfo struct { Url int32 } func WriteBin() { file,err := os.Create("outpath.bin") for i :=0;i<=10;i++{ info := WebInfo{ int32(i), } if err !=nil{ fmt.Println("file create failed err :",err.Error()) return } defer file.Close() var bin_buf bytes.Buffer binary.Write(&bin_buf,binary.LittleEndian,info) b := bin_buf.Bytes() _,err := file.Write(b) if err !=nil{ fmt.Println("编码失败",err.Error()) return } } fmt.Println("编码成功") } func ReadBin() { file,err:= os.Open("outpath.bin") defer file.Close() if err != nil { fmt.Println("文件打开失败", err.Error()) return } m := WebInfo{} for i :=0;i<=10;i++{ data := readNextBytes(file,4) buffer := bytes.NewBuffer(data) err = binary.Read(buffer,binary.LittleEndian,&m) if err !=nil{ fmt.Println("二进制文件读取失败,",err) return } fmt.Printf("第 %d 个值为:%v\n",i,m) } } func readNextBytes(file *os.File,num int) []byte { bytes := make([]byte,num) _,err := file.Read(bytes) if err !=nil{ fmt.Println("解码失败",err) } return bytes } func main() { //EncodeJson() //DecodeJson() //EncodeXml() //DecodeXml() //EncodeGob() //DecodeGob() //WriteBin() ReadBin() }