当前位置:网站首页>A brief explanation of golang's keyword "competence"
A brief explanation of golang's keyword "competence"
2022-04-23 19:21:00 【Handsome that handsome】
usage
be familiar with Golang Friends of json and struct The transition between must be no stranger , In order to compare the structure in the code with json Data decoupling , Usually we'll be in the... Of the structure field Type followed by explanation , For example, when representing an address , json The data is shown below
{
"street": "200 Larkin St",
"city": "San Francisco",
"state": "CA",
"zipcode": "94102"
}
Corresponding to that Golang The structure representation is defined as follows
type address struct {
Street string `json:"street"` // The street
Ste string `json:"suite"` // unit ( Can not exist )
City string `json:"city"` // City
State string `json:"state"` // state / province
Zipcode string `json:"zipcode"` // Zip code
}
So no matter how the variables in the code change , We can all succeed in json The data is parsed out , Get the right Street , City and other information , So far, everything's ok . But if we want to restore the address structure to json When the format , Here's the problem . For example, we read the address with the following code json , Then it is processed according to the business logic and returned to normal json Print out
func main() {
data := `{ "street": "200 Larkin St", "city": "San Francisco", "state": "CA", "zipcode": "94102" }`
addr := new(address)
json.Unmarshal([]byte(data), &addr)
// Dealt with it addr Variable ...
addressBytes, _ := json.MarshalIndent(addr, "", " ")
fmt.Printf("%s\n", string(addressBytes))
}
You can get the running results
{
"street": "200 Larkin St",
"suite": "",
"city": "San Francisco",
"state": "CA",
"zipcode": "94102"
}
One more line “suite”: “”, , And this message is in the original json There is nothing in the data ( In the address of the United States , If it's not a group apartment or shared office building , suite It's normal that this one doesn't exist , It's enough for people to express their address directly by street number ), But what we want more is , There are... In one address suite When the number is output , non-existent suite No output when , Fortunately, , We can do it in Golang Add... To the structure definition of omitempty keyword , To indicate that if this information is not provided , In sequence into json Don't include its default value when . Minor modifications , The address structure becomes
type address struct {
Street string `json:"street"`
Ste string `json:"suite,omitempty"`
City string `json:"city"`
State string `json:"state"`
Zipcode string `json:"zipcode"`
}
Rerun , You can get the correct result .
trap
Bring convenience at the same time , Use omitempty There are also some small traps , One is that the keyword cannot ignore the nested structure . Or take the address type , This time we want to add a new... To the address structure field To express latitude and longitude , If there is a lack of relevant data , Ignore it for the moment . The new structure definition is as follows
type address struct {
Street string `json:"street"`
Ste string `json:"suite,omitempty"`
City string `json:"city"`
State string `json:"state"`
Zipcode string `json:"zipcode"`
Coordinate coordinate `json:"coordinate,omitempty"`
}
type coordinate struct {
Lat float64 `json:"latitude"`
Lng float64 `json:"longitude"`
}
Read in the original address data , Serialize the output after processing , We'll find that even with omitempty keyword , Output json I still brought an empty coordinate information
{
"street": "200 Larkin St",
"city": "San Francisco",
"state": "CA",
"zipcode": "94102",
"coordinate": {
"latitude": 0,
"longitude": 0
}
}
In order to achieve the effect we want , You can define coordinates as pointer types , such Golang You can know the of a pointer “ Null value ” How much is it , Otherwise, we face a custom structure , Golang We can't guess the null value we want . So we have the following structure definition
type address struct {
Street string `json:"street"`
Ste string `json:"suite,omitempty"`
City string `json:"city"`
State string `json:"state"`
Zipcode string `json:"zipcode"`
Coordinate *coordinate `json:"coordinate,omitempty"`
}
type coordinate struct {
Lat float64 `json:"latitude"`
Lng float64 `json:"longitude"`
}
The corresponding output is
{
"street": "200 Larkin St",
"city": "San Francisco",
"state": "CA",
"zipcode": "94102"
}
the other one “ trap ” yes , For using omitempty Defined field , If the value assigned to it is exactly equal to the default null value , In turn json This will not be output later field . For example, the longitude and latitude coordinate structure defined above , If we take latitude and longitude two field All plus omitempty
type coordinate struct {
Lat float64 `json:"latitude,omitempty"`
Lng float64 `json:"longitude,omitempty"`
}
And then we're talking about the Gulf of Guinea in Africa “ Origin coordinates ” Very interested , So I wrote the following code
func main() {
cData := `{ "latitude": 0.0, "longitude": 0.0 }`
c := new(coordinate)
json.Unmarshal([]byte(cData), &c)
// Concrete processing logic ...
coordinateBytes, _ := json.MarshalIndent(c, "", " ")
fmt.Printf("%s\n", string(coordinateBytes))
}
Finally we got a
{
}
This coordinate disappeared ! But our assumption is , If a location has no longitude and latitude information , Then it hangs in the air , There's no problem with that , But for the “ Origin coordinates ”, When we know exactly its latitude and longitude ,(0.0, 0.0) Still ignored . The correct way to write it is to change the definition in the structure into a pointer
type coordinate struct {
Lat *float64 `json:"latitude,omitempty"`
Lng *float64 `json:"longitude,omitempty"`
}
In this way, the null value starts from float64 Of 0.0 Change to pointer type nil , We can see the correct latitude and longitude output .
{
"latitude": 0,
"longitude": 0
}
版权声明
本文为[Handsome that handsome]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210557450687.html
边栏推荐
- SSDB基础
- Easy mock local deployment (you need to experience three times in a crowded time. Li Zao will do the same as me. Love is like a festival mock)
- [报告] Microsoft :Application of deep learning methods in speech enhancement
- 坐标转换WGS-84 转 GCJ-02 和 GCJ-02转WGS-84
- Oracle configuration st_ geometry
- Pit encountered using camera x_ When onpause, the camera is not released, resulting in a black screen when it comes back
- Customize the non slidable viewpage and how to use it
- 腾讯云GPU最佳实践-使用jupyter pycharm远程开发训练
- static类变量快速入门
- Network protocol: SCTP flow control transmission protocol
猜你喜欢
First experience of using fluent canvas
Reflection on the performance of some OpenGL operations in the past
An algorithm problem was encountered during the interview_ Find the mirrored word pairs in the dictionary
MySQL syntax collation (4)
MySQL lock
OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
No, some people can't do the National Day avatar applet (you can open the traffic master and earn pocket money)
Application of DCT transform
An idea of rendering pipeline based on FBO
ArcMap connecting ArcGIS Server
随机推荐
Using oes texture + glsurfaceview + JNI to realize player picture processing based on OpenGL es
openlayers 5.0 两种居中方式
MySQL practical skills
Problems caused by flutter initialroute and home
Codeworks round 783 (Div. 2) d problem solution
Strange problems in FrameLayout view hierarchy
NiO related Basics
2021-2022-2 ACM集训队每周程序设计竞赛(8)题解
[记录]TypeError: this.getOptions is not a function
8266 obtain 18b20 temperature
SQL常用的命令
深度学习环境搭建步骤—gpu
Matlab 2019 installation of deep learning toolbox model for googlenet network
Openlayers 5.0 two centering methods
SSDB基础3
深度学习——特征工程小总结
FTP, SSH Remote Access and control
Openlayers 5.0 loading ArcGIS Server slice service
Coordinate conversion WGS-84 to gcj-02 and gcj-02 to WGS-84
Codeforces Round #784 (Div. 4)