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.