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 difference between type conversion and assertion in Go language

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "what is the difference between Go language type conversion and assertion". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The difference between type conversion and assertion

We know that implicit type conversions are not allowed in the Go language, that is, variables of different types are not allowed on both sides.

Type conversions and type assertions are essentially about converting one type to another. The difference is that type assertions are operations on interface variables.

Type conversion

For type conversion, the two types before and after conversion need to be compatible with each other. The syntax for type conversion is:

= ()

Assertion

As mentioned earlier, because the empty interface interface {} does not define any functions, all types in Go implement null interfaces. When the formal parameter of a function is interface {}, then in the function, the formal parameter needs to be asserted to get its true type.

The syntax for assertions is:

/ / Security type assertion

, =. (target type)

/ / non-safe type assertion

=. (target type)

Type conversions are somewhat similar to type assertions, except that type assertions are operations on interfaces.

In fact, there is another form of assertion, which is used to determine the type of interface using the switch statement. Each case is considered sequentially. When a case is hit, the statements in the case are executed, so the order of the case statements is important because there are likely to be multiple case matches.

The argument to the fmt.Println function is interface. For the built-in type, an exhaustive method is used inside the function to get its real type, and then converted to a string to print. For a custom type, first determine whether the type implements the String () method, and if so, print out the result of the String () method directly; otherwise, it traverses the members of the object through reflection to print.

Type T only has methods where the recipient is T, while type * T has methods whose recipients are T and * T. Grammatically, the way that T can directly adjust * T is only the grammatical sugar of Go.

Therefore, when the Student structure defines the String () method in which the recipient type is a value type, the

Can be printed in a custom format.

If the Student structure defines a String () method whose recipient type is a pointer type, only through the

To print in a custom format.

That's all for "what's the difference between Go language type conversion and assertion". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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