Go语言入门到实战-Struct 结构体

1、概述

结构体是将零个或多个任意类型的变量,组合在一起的聚合数据类型,也可以看做是数据的集合。

2、声明go struct结构体

//demo_11.go
package main

import (
	"fmt"
)

type Person struct {
	Name string
	Age int
}

func main() {
	var p1 Person
	p1.Name = "Tom"
	p1.Age  = 30
	fmt.Println("p1 =", p1)

	var p2 = Person{Name:"Burke", Age:31}
	fmt.Println("p2 =", p2)

	p3 := Person{Name:"Aaron", Age:32}
	fmt.Println("p2 =", p3)
	
	//匿名结构体
	p4 := struct {
		Name string
		Age int
	} {Name:"匿名", Age:33}
	fmt.Println("p4 =", p4)
}

运行结果:

$ go run demo1.go 
p1 = {Tom 30}
p2 = {Burke 31}
p2 = {Aaron 32}
p4 = {匿名 33}

3、go结构体转换成JSON

//demo_12.go
package main

import (
	"encoding/json"
	"fmt"
)

type Result struct {
	Code    int    `json:"code"`
	Message string `json:"msg"`
}

func main() {
	var res Result
	res.Code    = 200
	res.Message = "success"

	//序列化
	jsons, errs := json.Marshal(res)
	if errs != nil {
		fmt.Println("json marshal error:", errs)
	}
	fmt.Println("json data :", string(jsons))

	//反序列化
	var res2 Result
	errs = json.Unmarshal(jsons, &res2)
	if errs != nil {
		fmt.Println("json unmarshal error:", errs)
	}
	fmt.Println("res2 :", res2)
}

运行结果:

$ go run aa.go 
json data : {"code":200,"msg":"success"}
res2 : {200 success}

4、修改go结构体数据

//demo_13.go
package main

import (
	"encoding/json"
	"fmt"
)

type Result struct {
	Code    int    `json:"code"`
	Message string `json:"msg"`
}

func main() {
	var res Result
	res.Code    = 200
	res.Message = "success"
	toJson(&res)
	
	setData(&res)
	toJson(&res)
}

func setData (res *Result) {
	res.Code    = 500
	res.Message = "fail"
}

func toJson (res *Result) {
	jsons, errs := json.Marshal(res)
	if errs != nil {
		fmt.Println("json marshal error:", errs)
	}
	fmt.Println("json data :", string(jsons))
}

运行结果:

$ go run demo3.go 
json data : {"code":200,"msg":"success"}
json data : {"code":500,"msg":"fail"}

欢迎大家关注我的公众号:

389840-20200325153244909-853795575.png


© 版权归「好专栏」和作者所有,未经许可不得转载,本文地址:https://www.haozhuanlan.com/p/138