标签:pre split 一个 序列 imp 数据类型 描述 odi ict
内存中变成可存储或传输的过程称之为序列化(dict,split,struct转string)
package main import ( "encoding/json" "fmt" ) type Stu struct { Name string Age int } func JsonDump() (ret string, err error) { stu1 := Stu{ Name: "somebody", Age: 20, } data, err := json.Marshal(stu1) if err != nil { err = fmt.Errorf("json dump failed:", err) return } ret = string(data) fmt.Println(ret) return } func main() { JsonDump() }
磁盘读取到内存
需要先定义一个存储的数据类型(map struct),string要转成byte
package main import ( "encoding/json" "fmt" ) type Stu struct { Name string Age int } func JsonDump() (ret string, err error) { stu1 := Stu{ Name: "somebody", Age: 20, } data, err := json.Marshal(stu1) if err != nil { err = fmt.Errorf("json dump failed:", err) return } ret = string(data) return } func JsonLoad(res string) (ret interface{}, err error) { var stu Stu err = json.Unmarshal([]byte(res), &stu) if err != nil { err = fmt.Errorf("json loads failed:", err) return } ret = stu return } func main() { response, _ := JsonDump() res, _ := JsonLoad(response) fmt.Println(res) }
flag模块
package main import ( "flag" "fmt" ) func main() { var confPath string var command string var port int flag.StringVar(&confPath, "c", "conf", "conf file of app")//变量名 -c 默认值 描述 flag.StringVar(&command, "a", "online", "command to execute") flag.IntVar(&port, "p", 10000, "port of service") flag.Parse() fmt.Println(confPath) fmt.Println(command) fmt.Println(port) }
标签:pre split 一个 序列 imp 数据类型 描述 odi ict
原文地址:https://www.cnblogs.com/hongpeng0209/p/9172399.html