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 are the problems that may be encountered in loading json in go language

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

Share

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

What are the problems that go language may encounter when loading json? I believe many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problems. Through this article, I hope you can solve this problem.

Brief introduction to the problem

Go language is a simple but meaningful language. As we all know, the go language standard library has built-in processing of json files, which is very convenient. Recently, when writing an application, you need to load the configuration from the json file. Because you are a novice in go, ignoring a detail, the loading content is always empty. Let's take a look at the detailed introduction.

Code demonstration

The code is the best carrier of instructions.

Package configtype config struct {a string `json: "a" `b string `json: "a"`} func Load (file string) (* config, error) {c = & config {} file, err: = os.Open (file) if err! = nil {/ / file open failed todo} jsonParser: = json.NewDecoder (file) err = jsonParser.Decode (c) / c what is the value}

Conclusion

When you see the above code, you will think whether the assignment of c is successful?

In fact, unsuccessful, the parsing of the json file succeeded but the final assignment failed

The reason is actually very simple:

Go variable function method access in different packages. What distinguishes access permission is whether the first letter of the variable function method is capitalized. If it is uppercase, it can be accessed in other packages.

In this problem, json package and config are not the same package, access failed, cannot set

A b must be rewritten to uppercase before it can be assigned.

After reading the above, have you mastered the methods of the problems that may be encountered in loading json in go language? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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