当前位置:网站首页>How to declare the default value in go language structure and how to convert it into JSON data
How to declare the default value in go language structure and how to convert it into JSON data
2022-07-22 18:01:00 【Yard farmer is by my side】
go Language How the structure declares the default value How to translate into json data
// PanType Definition type
type PanType struct {
Name string `json:"name"`
Password string `json:"password"`
Children ChildrenPan `json:"children"`
}
// ErrorMsg Definition type
type ErrorMsg struct {
//json Followed by an alias
Code int `json:"code"`
Msg string `json:"msg"`
}
// Default initial value
func newPanType(name string) PanType {
str := PanType{
Name: name,
Password: "wqtqw",
}
return str
}
// Initialization error code
var msgErrorCode int = 4000
// Package error json
func newErrorMsg() bool {
// Get error messages
msg := recover()
// Force format to string
str2 := fmt.Sprintf("%s", msg)
str := ErrorMsg{
Code: msgErrorCode,
Msg: str2,
}
fmt.Println(msgErrorCode)
jsonStr, _ := json.Marshal(str)
jsonString := string(jsonStr)
fmt.Println(jsonString)
return true
}
func main() {
// Code execution finished Execution method
defer newErrorMsg()
// The default method
str := newPanType("jump")
// Modify the default password of the structure
str.Password = "easy_password"
// Turn into Json character string
jsonStr, errMsg := json.Marshal(str)
// Intentional error code
errMsg = errors.New("error")
if errMsg != nil {
// Modify the error code of the global variable
msgErrorCode = 4001
// Rewrite the wrong name
errMsg = errors.New("update error")
// Stop running Throw out
panic(errMsg)
}
fmt.Println(string(jsonStr))
}
边栏推荐
- mysql存储过程返回的结果集的问题
- JS String charAt substring() substr slice toUpperCase toLowerCase indexOf
- 【HMS core】【Health Kit】【FAQ】数据订阅功能问题合集
- 缓动动画、窗口相关数据和操作、BOM操作【DOM(五)】
- Pytorch实现Word2Vec
- Conference OA project
- [must see for developers] [push kit] collection of typical problems of push service 1
- Prepare for the attack and defense drill. Here is a security deployment map of Tencent!
- 02-线性结构2 一元多项式的乘法与加法运算(链表求解)
- C language branch structure and loop structure (1)
猜你喜欢
Methods of downloading literature from IEEE
How to carry out efficient data governance? Index management and data traceability help!
【OpenCV入门实战】利用电脑前置摄像头进行人脸检测
【3D目标检测】稀疏卷积
[HMS core] [FAQ] [account kit] typical problem set 1
Oracle statement adjustment
Web3 traffic aggregation platform starfish OS gives players a new paradigm experience of metauniverse
基础SQL Server 操作问题——仅当使用了列表并且IDENTITY_INSERT为ON时,才能为表中的标识列制定显示值
深度学习(二)一文带你了解神经网络,激活函数
日期类的理解学习
随机推荐
数据湖(十八):Flink与Iceberg整合SQL API操作
RK3399平台开发系列讲解(input子系统)4.52、input子系统的实现原理
How does win11 close the touch pad? Three solutions for closing the touch panel in win11
【数据库】MySQL表的增删改查(基础)
MySQL和MariaDB区别
Replace spaces in niuke.com
[HMS core] [FAQ] in app purchases FAQ sharing
深度学习(二)一文带你了解神经网络,激活函数
Linear regression (formula derivation +numpy Implementation)
【云原生】Docker部署数据库的持久化
基础SQL Server 操作问题——仅当使用了列表并且IDENTITY_INSERT为ON时,才能为表中的标识列制定显示值
subprocess
ieee下载文献的方法
[harmony OS] [ark UI] [demo] loading animation
RK3399平台开发系列讲解(ALSA子系统)4.37、ALSA驱动框架
Using dichotomy to find the elements of an array
Bigder:40/100 how to organize a use case review
网络之物理层
17、 C function pointer and callback function
Conference OA project introduction & Conference release