Go Language Summary (5)--type conversion and type assertion

Source: Internet
Author: User

Previous blog Introduction to the Go language arrays and slices--go language Summary (4)-Mapping (map), this blog introduction to the Go language type conversion and type assertion

Because the go language does not allow implicit type conversions. The nature of type conversions and type assertions is to convert one type to another type.

One, type conversion

(1), syntax :< result type >: = < target type > (< expression >)

(2), type conversions are used to convert between different but mutually compatible types , so when the type is incompatible, it cannot be converted. As follows:

func test4 () {varVar1int=7FMT. Printf ("%t->%v\n", var1, var1) var2:=float32 (var1) Var3:=Int64 (VAR1)//VAR4: = []int8 (var1)//VAR5: = []string (var1)Fmt. Printf ("%t->%v\n", Var2, var2) fmt. Printf ("%t->%v\n", Var3, VAR3)//FMT. Printf ("%t->%d", VAR4, VAR4)//FMT. Printf ("%t->%d", Var5, VAR5)}

Among them, VAR4 and VAR5 at the operation will be error. Because the type is incompatible. After commenting, the output is as follows:

7Int64,7float32, int--7

It is important to note that if some types can cause misunderstandings, they should be converted in parentheses, as follows:

func test5 () {    // creates an int variable and obtains its pointer to the    new(Int32)    FMT. Printf ("%t->%v\n", Var1, var1)    var2:= *int32 (VAR1)    FMT. Printf ("%t->%v\n", Var2, var2)}

*int32 (VAR1) is equivalent to * (Int32 (VAR1)), a pointer, of course, cannot be converted directly to a int32 type, so the expression compiles incorrectly. Change the expression to (*int32) (VAR1) to output normally.

Ii. Type Assertion

(1) Syntax:

< value of target type >,< boolean parameter >: = < expression >. (target type)//Security type Assertion

< The value of the target type >: = < expression >. (target type)//non-security type assertion

(2) The nature of the type assertion, similar to the type conversion, is the conversion between types, except that the type assertion is actually performed between interfaces, equivalent to Java, to an object that converts one interface reference to another.

Let's take a look at one of the simplest error type assertions:

func Test6 () {    varinterface"kk"    J:= I. ( int )    FMT. Printf ("%t->%d\n", J, J)}

var i interface{} = "KK" is in some way equivalent to Java, Object i = "KK";

Now convert this I to int type, the system detects this mismatch, it calls the built-in panic () function, throws an exception.

To change the definition of I to: var i interface{} = 99, there is no problem. The output is:

int-~

The above is an unsafe type assertion. Let's take a look at the security type assertion:

func Test6 () {    varinterface"TT"    J, b:= i. ( int )    if  B {        fmt. Printf ("%t->%d\n", J, J)    Else  {        Fmt. Println (" type mismatch ")    }}

Output "Type Mismatch".

Go Language Summary (5)--type conversion and type assertion

Related Article

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.