Go Language Learning-type

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

The Go language contains the following basic types:

Boolean type: BOOL.

Integral type: int8, Byte, int16, int, uint, uintptr, etc.

Floating-point types: float32, float64.

Plural type: complex64, complex128.

String: String.

Character type: Rune.

Fault type: Error.

In addition, the go language supports the following composite types:

Pointer (pointer)

Arrays (Array)

Slicing (slice)

Dictionary (map)

Channel (Chan)

struct (struct)

Interface (interface)

1. Type key word

The go language supports the Type keyword, which can define a type that is broadly similar to a typedef of the C language, but has a different key point: not the same type .

The format is as follows:

type int int

The above statement, which defines a new type int, has the same function as int, but it is not the same type and cannot be assigned directly, as the following code compiles an error:

var tmpint Int

Tmpint = 3

The above "int = 3" statement compiles an error because 3 defaults to int, and int is not the same type and can be cast to achieve the above assignment:

Tmpint = Int (3)

2. Boolean type

The assignable values are predefined true and false. Such as:

VAR B1 bool

B1 = True

B2: = (1 = = 2)//b2 is deduced as type bool

Note: Boolean types cannot be assigned to other types, nor do they accept automatic or forced type conversions.

3. Integral type

Predefined types are different, such as on 32-bit machines, where int and int32 are the same data types, do not automate type conversions, and, if necessary, can be used to solve problems such as assignment or logical comparisons with coercion type conversions. Such as:

var i1 int32

I2: = 5

I1 = i2//Compile error, I1 and I2 are int32 and int types, and cannot be converted automatically

I1 = Int32 (I2)//Correct assignment statement, cast I2 to Int32 to generate a temporary int32 value, and then pass to I1 (the type of i2 is still int and will not change).

The logical comparison is also the case where the following code fails to compile because the values of 2 different types cannot be compared:

if I1 = = i2 {

Fmt. Println ("i1== i2")


The above situation can also be resolved by forcing the type conversion.

Note: variables of various int types can be compared with literal constants (literal).

Note: The operation of integer data, including +-*/% and shift operations, and C completely The same. The only difference is thatthe negation operation in C is ~i, but the go language is ^i.

4. Interface (interface):

The interface in the go language is very special and almost overturns the idea of an object-oriented programming language, specifically analyzed.

5. Several other types:

Several other types and python in the comparison, usually use a little, it is not detailed analysis.

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.