go-Type Assertion

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

In Java there are instanceof such as the keyword judgment type go in the natural also have a corresponding method to determine the type

That is, the Comma-ok assertion

The notation is value, OK: = em. (T) If you make sure that EM is of the same type, you can use Value:=em directly. (T) commonly used in switch statements will be explained below

EM represents the variables to be judged

T stands for the type being judged

Value represents the returned values

OK indicates if the type is changed


Type assertion should be understood at a glance here will not introduce the main is the introduction of my own several problems

1 em must be of type initerface for type assertion

For example, the following code will error

S: = "Brainwu" if V, OK: = S. (string); OK {fmt. Println (v)}

Invalid type assertion:s. (string) (Non-interface type string on left)

In this case, the type assertion is a return non-interface error, as long as the arguments that are passed in the declaration or function are not interface types.

So we can only make type assertions by using S as a interface{} method, as shown in the following code

S: = "Brainwu" if V, OK: = interface{} (s). (string); OK {fmt. Println (v)}
converting s display to interface{} interface type can be type asserted.


2 When the function is a parameter and the called function specifies the parameter type as interface{}, there is no way to call the method directly

For example, the following code is wrong and will be error during compilation

Cannot convert in (type interface {}) to type Handler:need type assertion

Func servehttp (s string) {Fmt. Println (s)}type Handler func (String) func Panduan (in interface{}) {Handler (in) ("Wujunbin")}func Main () {Panduan ( Handler (servehttp))}

According to the error, we have to make the type assertion before we can continue to use this type of function

If V, OK: = in. (Handler); OK {//with what type to judge can only invoke what type of method V ("Brainwu")}
Only allow incoming in parameters to be typed with handler first if the return value is OK means the same type to make the corresponding method call

In addition to the type assertion, if the assertion succeeds, only the method of that type is used, such as for a struct s and a interface assertion

s actually implemented a B two interface

A interface has a () method B interface has a B () method if the struct S is passed into a function as a parameter and is a interface{} type in the function

Then the type assertion with a is only called a () and cannot be called B () because the compiler knows that you are currently a type but you do not know that you are currently type B


3 In addition, it is convenient to explain the combination of switch and type assertion.

Like the following example

Package Mainimport ("FMT") type Element interface {}func main () {var e Element = 100switch Value: = E. (type) {case int:fmt. PRINTLN ("int", value) case string:fmt. Println ("string", value) default:fmt. Println ("Unknown", Value)}}

Type is a keyword

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.