Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of a short horizontal line in Golang and Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is the role of a short horizontal line in Golang and Python". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the role of a short horizontal line in Golang and Python"?

Do not output 0 before date time

When we use Python to output time and date, the general format is as follows: YYYY-mm-dd HH:MM:SS, for example: 2021-03-22 09:10:12. You can see that there is a 0 in front of 03 and 09. The advantage of this is that you can keep the date and time the same length, so you can compare the size by string.

But sometimes, for some reason, you may not really want to make up 0, and you just want to output time in the format of 2021-3-22 9:10:12.

But we know that formatting the date and time output in Python is as follows:

Import datetime now = datetime.datetime.now () dt_str = now.strftime ('% Y-%m-%d% HRV% MRV% S')

In this formatting symbol, we can see that we are using a character, the month is% m, is there another symbol to indicate the date that does not make up 0?

In fact, not adding zero does not need another symbol to indicate, we just need to add a short dash between% and m:

Import datetime now = datetime.datetime.now () dt_str = now.strftime ('% Y-%-m-%-d%-HGV% muri MRV% Muz S') print (dt_str)

The running effect is shown in the following figure:

Mutual conversion between Golang structure and JSON

When using Golang, we sometimes need to convert between structs and JSON strings. In the conversion project, you may find that some fields are redundant. We don't want some fields in the JSON to go to the structure, or we don't want to transfer some fields in the structure to the JSON.

For moving from JSON to structure, it is very simple to ignore fields, such as the following JSON string:

{"name": "kingname", "salary": 999999999, "address": "Shanghai", "handsome": true}

When I convert to a structure, I don't want the handsome field, so I can write the code like this:

Package main import ("encoding/json"fmt") type Info struct {Name string `json: "name" `Salary int64 `json: "salary" `Address string `json: "address" `} func main () {jsonstr: =` {"name": "kingname", "salary": 999999999, "address": "Shanghai", "handsome": true} `var info Info err: = json.Unmarshal ([] byte (jsonstr)) & info) if err! = nil {panic (err)} fmt.Println (info)}

The running effect is shown in the following figure:

But the question is, what if I give you a structure now, but I don't want one of the fields when I convert it to JSON? Some people may think that when defining the fruit body, do not add json: "xxx" is fine. However, the actual implementation effect is shown in the following figure:

Golang will directly use the name of this field in the structure to convert to JSON.

If you don't want this field, there is actually a very simple way to define the structure with json: "-", for example:

Type Info struct {Name string `json: "name" `Salary int64 `json: "salary" `Address string `json: "address" `Handsome bool `json: "-" `}

The running effect is shown in the following figure:

By specifying the line, you can ignore this field.

Thank you for your reading, the above is "what is the role of a short horizontal line in Golang and Python", after the study of this article, I believe you have a deeper understanding of the role of a short horizontal line in Golang and Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report