Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 05:13:48 10月03日 Update

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!

Tags: Field structure horizontal line time one date time function character effect format output string actual that is symbol learn run between code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Huawei OPPO Reno vpn NVidia