JSON
Go语言内置的encoding/json 标准库
插件:github.com/pquerna/ffjson
JSON编码:func Marshal(v interface{}) ([]byte, error)
JSON解码:func Unmarshal(data []byte, v interface{}) error
- package main
-
- import (
- "encoding/json"
- "fmt"
- )
-
- type Student struct {
- Name string `json:"student_name"`
- Age int
- }
-
- func main() {
- //对数组类型的json编码
- x := [5]int{1, 2, 3, 4, 5}
- s, err := json.Marshal(x)
- if err != nil {
- panic(err)
- }
-
- fmt.Println(string(s))
-
- //对map类型进行json编码
- m := make(map[string]float64)
- m["zhangsan"] = 100.4
- s2, err2 := json.Marshal(m)
- if err2 != nil {
- panic(err2)
- }
-
- fmt.Println(string(s2))
-
- //对对象进行json编码
- student := Student{"zhangsan", 26}
- s3, err3 := json.Marshal(student)
- if err3 != nil {
- panic(err3)
- }
-
- fmt.Println(string(s3))
-
- //对s3进行json解码
- var s4 interface{}
- json.Unmarshal([]byte(s3), &s4)
- fmt.Printf("%v", s4)
- }
MD5
Go语言内置的crypto/md5 标准库
Md5Inst:=md5.New()
Md5Inst.Write([]byte(“test md5”))
Result:=Md5Inst.Sum([]byte(""))
fmt.Printf("%x\n\n",Result)
- package main
-
- import (
- "crypto/md5"
- "fmt"
- )
-
- func main() {
- Md5Inst := md5.New()
- Md5Inst.Write([]byte("zhangsan"))
- Result := Md5Inst.Sum([]byte(""))
- fmt.Printf("%x\n\n", Result)
- }