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

Can var in go initialize structures?

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

Share

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

This article mainly explains "can the var in go initialize the structure", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "can the var in go initialize the structure?"

Understand the grammar in advance

Convert string to int using the strconv.Atoi method in golang

There can be multiple return values for a method in golang, which is different from java. For example, in the way of mapping [id], the first return value is value, and the second return value is of type bool, indicating whether it exists.

Similar to the way json is serialized in java, deserialization is the json.Unmarshal method. The first parameter is the byte array, and the second parameter is the "address" of the serialized object. Add a &, the serialization way json.Marshal method. Note the byte array returned by this method.

The grammar used in the above section is often used in the following exercises. Friends can simply memorize it.

Practice

1. We simply define a User class and use golang to add, delete, change and query the instance of this structure. we define a structure as follows, with three properties Id,Name,Age.

two。 Because we add, delete, change and check in memory and do not use the database, we need to define a global variable as a container to store data like java. As shown in the source code of the following figure, we use the keyword var to define a global variable map and initialize a record

3. Then define the four "RequestMapping" as follows

3.1 the main logic of the getUser method is to get the passed parameter id, and then go to the global variable according to id to get the basic information of user

3.2 saveUser We handle POST requests. Now we parse the json data passed through the development process to the corresponding json, and then save it to our global variables.

3.3.The updateUser is similar to saveUpdate. For simplicity, we only judge that if the corresponding id in the global map exists, we will change the user object corresponding to this id without attribute comparison and replacement.

3.4 deleteUser is even easier. Just delete the corresponding id in the map.

test

Testing of 1.getUser

Testing of 2.saveUser

Testing of 3.updateUser

4.

At this point, I believe you have a deeper understanding of "can the var in go initialize the structure?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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