This is a creation in Article, where the information may have evolved or changed.
Type System
Most of the types in Go are value semantics and can contain corresponding action methods, and you can add new methods to the type as needed. When implementing an interface, there is no need to inherit from the interface (in fact go does not support object-oriented integration syntax), just implement all the methods required by the interface. any type can be referenced by any type. The any type is an empty interface, or interface{}.
Add a method to a type
You can add methods to any type
Type Integer intfunc (a integer) less (b integer) bool { return a < B}var a integer = 1if a.less (2) { FMT. Println (A, "Less 2")}
The "This pointer is not hidden in the go language" is the meaning of the phrase:
The target (i.e. "object") applied by the method is explicitly passed, and is not hidden;
The target (i.e. "object") applied by the method does not need to be a pointer, nor does it have to be called this.
Value semantics and referential semantics
The difference is that the assignment
b = ab. Modify ()
B's modification does not affect a, it is the value semantics, otherwise it is cited as meaning
b = &ab. Modify ()
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). There are 4 types in the go language that are more specific and look like reference types as follows:
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
Channel and map are similar, essentially a pointer
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, and retains only the most basic features of the combination (composition).
Type Rect struct { x, y float64 width, height float64}//definition method func (r *rect) area () float64 { return r.width * r . Height}
It looks a lot like C, and the fact is, the way it's used doesn't differ significantly from the C language.
Initialization
Rect1: = new (Rect) Rect2: = &rect{}rect3: = &rect{0, 0, 200}rect4: = &rect{width:100, height:200}
A variable that is not explicitly initialized is initialized to a value of 0 for that type: BOOL->false int->0 string---empty string There is no concept of constructors in the go language, and the creation of objects is usually done by a global creation function to
Newxxx to name, which means "constructor":
Func newrect (x, y, width, height float64) *rect { return &rect{x, y, width, height}}
Anonymous combination
To be exact, the go language also provides inheritance, but employs a combination of grammars, so we call it an anonymous combination:
Type base struct { Name string}func (base *base) Foo () {...} Func (base *base) Bar () {...} Type Foo struct { Base ...} Func (foo *foo) Bar () { foo. Base.bar () ... }
Above, the base class is defined, the Foo and bar methods are implemented, the Foo class is defined, the bar is inherited and rewritten, and the Foo is inherited without changing it. In the go language, you can also "derive" from a type as a pointer:
Type Foo struct { *base ... }
look at the name collisions in the interface combination
Type X struct { name string}type Y struct { X name string}
As in the example above, there is a member name in the Y type, and its anonymous combo x also has the name of the same name, but all Y-type Access members name is accessible only to the outer layer, and name in the X class is hidden. Look again:
Type Logger struct {level int}type Y struct { *logger Name string *log. Logger}
If not, the anonymous combination can be thought of as the name of the member variable with its type name (minus the package name part), there will be two Logger with the same name. That is not to be