Use of Golang assertions (Type assertion)

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

The assertion functionality is provided in the Golang language. All programs in Golang implement the interface of interface{}, which means that all types, such as string,int,int64 or even custom struct types, have an interface for interface{}. This approach is similar to the object type in Java. So when a data is passed through the Func FuncName (interface{}), it means that the parameter is automatically converted to the type of interface{}.

As in the following code:

Func FuncName (A interface{}) string {     return string (a)}

The compiler will return:

Cannot convert a (type interface{}) to type String:need type assertion

At this point, it means that the entire conversion process requires a type assertion. Type assertions are available in the following ways:

1) Direct assertion using

var a interface{}

Fmt. Println ("Where is You,jonny?", A. (string))

However, if the assertion fails, it generally causes panic to occur. So in order to prevent panic from happening, we need to make certain judgments before the assertion.

Value, OK: = A. (string)

If the assertion fails, then the OK value will be false, but if the assertion succeeds the value of OK will be true, and value will get the correct value expected. Example:

Value, OK: = A. (string) If!ok {    fmt. Println ("It ' s not ok for type string")    return}fmt. Println ("The value is", value)

Alternatively, you can also match the switch statement to determine:

var t interface{}t = functionofsometype () switch t: = T. (type) {default:    FMT. Printf ("Unexpected type%T", T)       //%T prints whatever type T hascase bool:    FMT. Printf ("Boolean%t\n", T)             //T has type Boolcase int:    FMT. Printf ("integer%d\n", T)             //T has type Intcase *bool:    fmt. Printf ("Pointer to Boolean%t\n", *t)//t have type *boolcase *int:    fmt. Printf ("Pointer to Integer%d\n", *t)//t have type *int}

Plus a few tips to add some go language programming

1) If you do not meet the requirements can be as soon as possible return (return as fast as you can), and reduce the use of else statements, this can be more intuitive.

2) When converting types, use FMT if string can be used without assertions. The Sprint () function can achieve the desired effect

3) The definition and declaration of variables can be used in a group way, such as:

var (   a string   b int   c int64 ...   ) Import (    "FMT"    "strings" "    net/http" ...   )

4) The function logic is more complex, can encapsulate some logic into a function, improve the readability.

5) functions that use the Net/http package and the Net/url package may have a URL encode function that requires attention


Reference content

Http://golang.org/doc/effective_go.html?h=type+assertion

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.