This is a creation in Article, where the information may have evolved or changed.
Order:
In the Java language, there are two completely separate types of systems: a set of value type systems, primarily basic types, such as a byte int boolean char double, which is based on value semantics; These types can define member variables and member methods, and can have virtual functions that are based on referential semantics and are only allowed to be created on the heap (by using the keyword new). The any type in the Java language is the root--java.lang.object type of the entire object system, and only instances in the object type system can be referenced by any type. A value type that wants to be referenced by any type requires a boxed (boxing) procedure, such as an int type that needs to be boxed into an integer type. In addition, only types in the object type system can implement an interface, specifically by having the type inherit from the interface being implemented.
In contrast, most types in the go language are value semantics and can contain corresponding action methods. You can "add" new methods to any type (including built-in types) when needed. When you implement an interface, you do not need to integrate it from the interface (in fact, the go language does not support the inheritance syntax in object-oriented thinking at all), just implement all the methods required by that interface. Any type can be referenced by any type. The any type is an empty interface, which is interface ().
To 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:
Type Integer int
Func (a integer) less (b integer) bool {
Listen and hear return a < 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 enables the integer to be used as if it were a generic class:
Func Main () {
Listen and listen to the var a Integer = 1
Hear and hear if a.less (2) {
Listen, listen, hear, hear, and listen to the FMT. Println (A, "less 2")
Listen and Hear}
}
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.
Most types in the go language are based on value semantics, including:
Basic types, such as byte int bool float32 float64 and string
Composite types, such as array struct (struct) and pointer (pointer), etc.
The value semantics of the type in the go language are very thorough. The reason for this is that the array.
There is no difference between the array and the base type in the go language, which is a purely value type, for example:
var a = [3]int{1,2,3}
var B = A
b[1]++
Fmt. Println (A, B)
The results of this program are as follows:
[1 2 3] [1 3 3]
This indicates that the B=a assignment statement is a complete copy of the array contents. To express a reference, you need to use the pointer:
var a = [3]int{1, 2, 3}
var B = &a
b[1]++
Fmt. Println (A, *b)
The results of this program are as follows:
[1 3 3] [1 3 3]
This indicates that the B=&a assignment statement is a reference to the contents of the array. The type of variable B is not [3]int, but *[3]int type.
The 4 types in the Go language are special and look like reference types, as shown below.
Array Slice: An interval that points to an array.
Map: A very common data structure that provides key-value query capabilities.
Channel: Communication facility between the executing body (goroutine).
Interface (interface): An abstraction of a set of types that satisfy a contract.
However, this does not affect the use of the Go language type as value semantics. Let's take a look at these 4 types.
An array slice is essentially an interval, and you can roughly represent []t as:
Type slice struct {
Listen and hear first *t
Listen and hear Len int
Listen listen to the cap int
}
Because array slices are pointers to arrays, it is not surprising that you can change the array elements that you point to. The assignment of the array slice type itself is still value semantics.
Structure:
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 integration, and retains only the most basic features of the combination (composition).
As we said above, all the Go language types (except pointer types) can have their own methods. In this context, the structure of the go language is only a very common composite type, bland. For example, we want to define a rectangular type:
Type Rect struct {
Listen, listen, x, y float64.
Listen and listen to width, height float64
}
We then define the Member method area () to calculate the size of the rectangle:
Func (R *rect) area () float64 {
Listen and hear return r.width * r.height
}