惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Google DeepMind News
Google DeepMind News
J
Java Code Geeks
S
SegmentFault 最新的问题
Martin Fowler
Martin Fowler
罗磊的独立博客
T
The Blog of Author Tim Ferriss
N
Netflix TechBlog - Medium
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Heimdal Security Blog
N
News and Events Feed by Topic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy International News Feed
T
Tailwind CSS Blog
AWS News Blog
AWS News Blog
雷峰网
雷峰网
PCI Perspectives
PCI Perspectives
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
N
News | PayPal Newsroom
C
CERT Recently Published Vulnerability Notes
Microsoft Security Blog
Microsoft Security Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
博客园 - 【当耐特】
Vercel News
Vercel News
GbyAI
GbyAI
博客园 - 司徒正美
量子位
T
Threat Research - Cisco Blogs
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
MyScale Blog
MyScale Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Hacker News - Newest:
Hacker News - Newest: "LLM"
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
Y
Y Combinator Blog
P
Proofpoint News Feed
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research

jobcher on 打工人日志

2023-12-21 打工人日报 2023-12-20 打工人日报 2023-12-19 打工人日报 2023-12-18 打工人日报 2023-12-17 打工人日报 2023-12-16 打工人日报 2023-12-15 打工人日报 2023-12-14 打工人日报 2023-12-13 打工人日报 2023-12-12 打工人日报 2023-12-11 打工人日报 2023-12-10 打工人日报 2023-12-09 打工人日报 2023-12-08 打工人日报 2023-12-07 打工人日报 2023-12-06 打工人日报 2023-12-05 打工人日报 2023-12-04 打工人日报 2023-12-03 打工人日报 2023-12-02 打工人日报 2023-12-01 打工人日报 2023-11-30 打工人日报 2023-11-29 打工人日报 2023-11-28 打工人日报 2023-11-27 打工人日报 2023-11-26 打工人日报 2023-11-25 打工人日报 2023-11-24 打工人日报 2023-11-23 打工人日报 2023-11-22 打工人日报 2023-11-21 打工人日报 2023-11-20 打工人日报 2023-11-19 打工人日报 2023-11-17 打工人日报 2023-11-16 打工人日报 2023-11-15 打工人日报 2023-11-14 打工人日报 2023-11-13 打工人日报 2023-11-12 打工人日报 2023-11-11 打工人日报 2023-11-10 打工人日报 2023-11-09 打工人日报 2023-11-08 打工人日报 2023-11-07 打工人日报 2023-11-06 打工人日报 2023-11-05 打工人日报 2023-11-04 打工人日报 2023-11-03 打工人日报 2023-11-02 打工人日报 2023-11-01 打工人日报 2023-10-31 打工人日报 2023-10-30 打工人日报 2023-10-29 打工人日报 2023-10-28 打工人日报 2023-10-27 打工人日报 2023-10-26 打工人日报 2023-10-25 打工人日报 2023-10-24 打工人日报 2023-10-23 打工人日报 2023-10-22 打工人日报 2023-10-21 打工人日报 2023-10-20 打工人日报 2023-10-19 打工人日报 2023-10-18 打工人日报 2023-10-17 打工人日报 2023-10-16 打工人日报 2023-10-15 打工人日报 2023-10-14 打工人日报 2023-10-13 打工人日报 2023-10-12 打工人日报 2023-10-11 打工人日报 2023-10-10 打工人日报 2023-10-09 打工人日报 2023-10-07 打工人日报 2023-10-06 打工人日报 2023-10-05 打工人日报 2023-10-04 打工人日报 2023-10-03 打工人日报 2023-10-02 打工人日报 2023-10-01 打工人日报 2023-09-30 打工人日报 2023-09-29 打工人日报 2023-09-28 打工人日报 2023-09-27 打工人日报 2023-09-26 打工人日报 2023-09-25 打工人日报 2023-09-24 打工人日报 2023-09-23 打工人日报 2023-09-22 打工人日报 2023-09-21 打工人日报 2023-09-20 打工人日报 2023-09-19 打工人日报 2023-09-18 打工人日报 2023-09-17 打工人日报 2023-09-16 打工人日报 2023-09-15 打工人日报 2023-09-14 打工人日报 2023-09-13 打工人日报 2023-09-12 打工人日报 2023-09-11 打工人日报
go Struct 结构体
2022-04-26 · via jobcher on 打工人日志

go Struct 结构体

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

声明结构体

 1//demo_11.go
 2package main
 3
 4import (
 5	"fmt"
 6)
 7
 8type Person struct {
 9	Name string
10	Age int
11}
12
13func main() {
14	var p1 Person
15	p1.Name = "Tom"
16	p1.Age  = 30
17	fmt.Println("p1 =", p1)
18
19	var p2 = Person{Name:"Burke", Age:31}
20	fmt.Println("p2 =", p2)
21
22	p3 := Person{Name:"Aaron", Age:32}
23	fmt.Println("p2 =", p3)
24
25	//匿名结构体
26	p4 := struct {
27		Name string
28		Age int
29	} {Name:"匿名", Age:33}
30	fmt.Println("p4 =", p4)
31}

生成 JSON

 1//demo_12.go
 2package main
 3
 4import (
 5	"encoding/json"
 6	"fmt"
 7)
 8
 9type Result struct {
10	Code    int    `json:"code"`
11	Message string `json:"msg"`
12}
13
14func main() {
15	var res Result
16	res.Code    = 200
17	res.Message = "success"
18
19	//序列化
20	jsons, errs := json.Marshal(res)
21	if errs != nil {
22		fmt.Println("json marshal error:", errs)
23	}
24	fmt.Println("json data :", string(jsons))
25
26	//反序列化
27	var res2 Result
28	errs = json.Unmarshal(jsons, &res2)
29	if errs != nil {
30		fmt.Println("json unmarshal error:", errs)
31	}
32	fmt.Println("res2 :", res2)
33}

改变数据

 1//demo_13.go
 2package main
 3
 4import (
 5	"encoding/json"
 6	"fmt"
 7)
 8
 9type Result struct {
10	Code    int    `json:"code"`
11	Message string `json:"msg"`
12}
13
14func main() {
15	var res Result
16	res.Code    = 200
17	res.Message = "success"
18	toJson(&res)
19
20	setData(&res)
21	toJson(&res)
22}
23
24func setData (res *Result) {
25	res.Code    = 500
26	res.Message = "fail"
27}
28
29func toJson (res *Result) {
30	jsons, errs := json.Marshal(res)
31	if errs != nil {
32		fmt.Println("json marshal error:", errs)
33	}
34	fmt.Println("json data :", string(jsons))
35}