Go language type conversion and type assertion Instance Analysis _golang

Source: Internet
Author: User

The examples in this article describe the use of Go language type conversions and type assertions. Share to everyone for your reference. The specific analysis is as follows:

Because the go language does not allow implicit type conversions. The essence of type conversion and type assertion is to convert a 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 they cannot be converted when the type is incompatible. As follows:

Copy Code code as follows:
Func test4 () {
var var1 int = 7
Fmt. 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 run will be an error. Because the type is not compatible. After the annotation, the output is as follows:

Copy Code code as follows:
Int->7
Float32->7
Int64->7

It is noteworthy that if some types may cause misunderstandings, they should be enclosed in parentheses to convert the following:
Copy Code code as follows:
Func Test5 () {
Create an int variable and get a pointer to it
VAR1: = 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, can not be directly converted to a int32 type, so the expression directly compile the error. Change the expression to (*int32) (VAR1) to output normally.

Ii. Type Assertion

(1) Syntax:

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

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

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

Let's take a look at one of the simplest types of false assertions:

Copy Code code as follows:
Func Test6 () {
var i interface{} = "KK"
J: = I. (int)
Fmt. Printf ("%t->%d\n", J, J)
}

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

Now convert this I to type int, the system detects this mismatch internally, and the built-in panic () function is invoked to throw an exception.

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

Copy Code code as follows:
int->99

The above is an unsafe type assertion. Let's take a look at the security type assertion:
Copy Code code as follows:
Func Test6 () {
var i interface{} = "TT"
J, B: = I. (int)
If B {
Fmt. Printf ("%t->%d\n", J, J)
} else {
Fmt. Println ("Type Mismatch")
}
}

Output "Type Mismatch".

I hope this article will help you with your go language program.

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.