Type assertion parsing for Go

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Often we are uncertain about the dynamic type of an interface value, such as when the parameter of a method is an interface type, it is necessary to verify that it conforms to the type we need.
A type assertion is an operation that uses a value on an interface.

If you are unfamiliar with the concept of interface and interface values for Golang, see here: Go interface Summary
The syntax for the assertion type:x. (t), where x represents the type of an interface, and T represents a type (which can also be an interface type).
A type assertion checks whether the dynamic type of an interface object x matches the type T of the assertion.

There are two types of assertion:
First , if the asserted type T is a concrete type, the type asserts X. T) checks if the dynamic type of x is the same as the type of T.

    • If this check succeeds, the result of the type assertion is an object of type T, and the value of the object is the dynamic value of the interface variable x. In other words, a type assertion of a specific type obtains a specific value from its operand.
    • If the check fails, the next operation throws panic, unless two variables are used to receive the check result, such as: F, OK: = W. (*os). File)

The second , if the asserted type T is an interface type, the type asserts X. T) check if the dynamic type of x satisfies the T interface.

    • If this check succeeds, the dynamic type and dynamic value of the interface value of the check result is not changed, but the type of the interface value is converted to the interface type T. In other words, the type assertion of an interface type changes the way the type is expressed, changing the collection of methods that can be obtained (usually larger), but it protects the part of the dynamic type and value inside the interface value.
    • If the check fails, the next operation throws panic, unless two variables are used to receive the check result, such as: F, OK: = W. (IO. Readwriter)

Attention:

    • If the asserted operand x is a nil interface value, the type assertion will fail regardless of what type T is asserted.
    • We hardly need to assert a less restrictive interface type (fewer collection of methods) because it behaves like an assignment operation except in the case of nil interface values.

Example code:

  

= = = Interface =====type Tester interface {getName () String}type Tester2 interface {printname ()}//===person type ====type person struct {name String}func (P person) getName () string {return P.name}func (P-person) Printname () {fmt. Println (P.name)}//============func main () {var t testert = person{"Xiaohua"}check (t)}func check (t Tester)  {    // The first case is if f, ok1: = T. (person); Ok1 {fmt. Printf ("%t\n%s\n", F,f.getname ())}    //second case if T, ok2: = T. (Tester2); ok2 {  //reuse variable name T (without re-declaration) Check2 (t)// If the type assertion is true, the new T is transformed to the Tester2 interface type, but its dynamic type and dynamic value are unchanged}}func Check2 (t Tester2)  {t.printname ()}

Execution Result:

Main. Person
Xiaohua
Xiaohua

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.