微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/go/190940.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐