Type conversion of Go language

Source: Internet
Author: User

The go language requires the conversion to be displayed between different types. Conversions are divided into type conversions and interface conversions.

The idea of type conversion is:

The X type needs to be converted to the Y type, and the syntax is T (x). If you are unsure of the priority of some place, you can add () the constraint yourself to (T) (X). Such as:

*point (P)//and * (Point (P))

(*point) (p)//p converted to *point

(Func ()) (x)//x converted to UNC ()

(func () int) (x)//x convert func () int

Interface conversions

For example, there are 2 types of interfaces:

Type IA Interface {}

Type IB Interface {Foo ()}

IA to convert to IB:

var a A

var B = A. (b)

IB conversions like IA:

var b b

var a = a (b)

The difference between the above two conversions:

1. First realize that IB has a more than Ia method foo (), which is a subset of IA.

2, IA to IB, is from the general to the special conversion, not necessarily can succeed, so use assert a. (B), from the literal meaning of the word "assertion", it is understandable to "judge", not necessarily to succeed.

3, IB into IA, is a special to the general transformation, direct a (b) forced conversion can be.

The conversion between basic types such as String,int,bool in Go requires a StrConv package.

Official Document Address: http://golang.org/pkg/strconv/

Check the documentation when you use it.

Type conversion of Go language

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.