Golang(9)File Management

前端之家收集整理的这篇文章主要介绍了Golang(9)File Management前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Golang(9)File Management

7.1 XML
import (
“encoding/xml”
“fmt”
“io/IoUtil”
“os"
)

xml.Unmarshal(data []byte,v inteface{}) error
xml.Marshal(v interface{}) ([]byte,error)
xml.MarshalIndent(v interface{},prefix,indent string) ([]byte,error)

7.2 JSON Handling
JSON(Javascript Object Notation)

Here is the sample JSON string
{“servers”:[{“serverName”:”Shanghai_VPN”,“serverIP”:”127.0.0.1”},{“serverName”:”Beijing_VPN”,”serverIP”:”127.0.0.2"}]}

Parse JSON
func Unmarshal(data []byte,v interface{}) error

Here is the example how to use this function.
package main

import (
"encoding/json"
"fmt"
)

type Server struct {
ServerName string
ServerIP string
}

type Serverslice struct {
Servers []Server
}

func main() {
var s Serverslice
str := `{"servers":[{"serverName":"Shanghai_VPN","serverIP":"127.0.0.1"},{"serverName":"Beijing_VPN","serverIP":"127.0.0.2"}]}`
json.Unmarshal([]byte(str),&s)
fmt.Println(s)
fmt.Println(s.Servers[0])
fmt.Println(s.Servers[0].ServerIP)
}

Remember to capital the first property name.

bool <——>JSON booleans
float64 <———>JSON numbers
string <————>JSON strings
nil <————> JSON null

We can use bitly simplejson to parse the unknown format JSON string.
https://github.com/bitly/go-simplejson

>go get github.com/bitly/go-simplejson

package main

import (
"fmt"
"github.com/bitly/go-simplejson"
)

func main() {
js,_ := simplejson.NewJson([]byte(`{
"test": {
"array": [1,2,3],
"int": 10,
"float": 5.150,
"bignum": 9223372036854775807,
"string": "simplejson",
"bool": true
}
}`))

arr,_ := js.Get("test").Get("array").Array()
i,_ := js.Get("test").Get("int").Int()
ms := js.Get("test").Get("string").MustString()

fmt.Println(arr)
fmt.Println(i)
fmt.Println(ms)
}

The console output is as follow:
>go run src/com/sillycat/easygoapp/main.go
[1 2 3] 10 simplejson

Generate JSON
func Marshal(v interface{}) ([]byte,error)

package main

import (
"encoding/json"
"fmt"
)

type Server struct {
ServerName string
ServerIP string
}

type Serverslice struct {
Servers []Server
}

func main() {
var s Serverslice
s.Servers = append(s.Servers,Server{ServerName: "Shanghai_VPN",ServerIP: "127.0.0.1"})
s.Servers = append(s.Servers,Server{ServerName: "Beijing_VPN",ServerIP: "127.0.0.2"})
b,err := json.Marshal(s)
if err != nil {
fmt.Println("json err:",err)
}
fmt.Println(string(b))
}

The console output should be as follow:
{"Servers":[{"ServerName":"Shanghai_VPN","ServerIP":"127.0.0.1"},{"ServerName":"Beijing_VPN","ServerIP":"127.0.0.2"}]}

7.3 Regex
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/07.3.md

7.4 Template
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/07.4.md

7.5 File Operation
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/07.5.md

7.6 String Operation
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/07.6.md



References:
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/07.0.md@H_430_403@ @H_829_404@

猜你在找的Go相关文章