This is a creation in
Article, where the information may have evolved or changed.
Types System type Systems
Type system refers to the type architecture of a language. A typical type system typically contains the following basic elements:
Underlying types, such as byte, int, bool, float , and so on
Composite types, such as arrays, structs, pointers, etc.
Types that can point to any object (any type)
Value semantics and referential semantics
Object-oriented, that is, all types with object-oriented features (such as member methods)
Interface
The type system describes how the content is associated in a language.
Add a method to a type
In the go language, you can add methods to any type (including built-in types, but not pointer types), for example
TypeIntegerint
Func(aInteger)less(b Integer)bool{
returna<b
}
In this example, we define a new type integer, which is not fundamentally different from int, except that it adds a new method of less () to the built-in int type.
This allows the integer to be used like a normal class, after the integer is implemented:
FuncMain (){
var a Integer=1
ifa. Less(2){
fmt. Println(a,"less2")
}
}
value semantics and referential semantics
The difference between value semantics and referential semantics is the assignment of values, such as the following example:
b = A
B.modify ()
If the modification of B does not affect the value of a, then this type belongs to the value type. If the value of a is affected, then this type is a reference type.
Structural body
The Go language structure (struct) has the same status as other language classes (class), but the go language discards a large number of object-oriented features including inheritance, preserving only the most basic features of the composition.
As mentioned above, all the Go language types (except pointer types) can have their own methods. In this context, the structure of the go language is just a common compound type, for example, we want to define a rectangular type:
TypeRectstruct{
x,y float64
width,heightfloat64
}
We then define the Member method area () to calculate the size of the rectangle:
Func(r*Rect)area() Float64{
returnr. Width*r. Height
}