This is a creation in Article, where the information may have evolved or changed.
Objective:
The type system of the Go language
Basic types, such as Byte, int, bool, float, etc.;
Composite types, such as arrays, structures, pointers, etc.;
Can point to the type of 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 go language, like the C language, is based on value, and if you want to reference that value, you can reduce the space wasted by using pointers.
If you want to modify the value, you need to pass the pointer.
There are two completely separate types of systems in Java:
The set is a value type system, mainly basic types, such as Byte, int, Boolean, char, double, etc., based on value semantics;
A set is an object type system that is rooted in type objects, which can define member variables and member methods, can have virtual functions, are based on referential semantics, and are only allowed 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 type 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
Most types in the go language are value semantics and can contain corresponding action methods.
Any type can be referenced by any type. Any type is an empty interface, that is, interface{}
While implementing an interface, you do not have to inherit 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
To add a method to a type:
Type Integer intfunc (a integer) less (b integer) bool {//Object-oriented return a < b} func integer_less (a integer, b integer) b ool {//process-oriented return a < b} a.less (2)//object-oriented usage integer_less (A, 2)//process-oriented usage
The go language, like the C language, is a type that is passed on value. To modify the value of a variable, you can only pass pointers
Func (a *integer) Add (b Integer) {*a + = b}
value semantics and referential semantics:
Most of the go languages are value types, but some exceptions
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.
Because it's essentially an interval.
Array slices
Type slice struct {first *tlen intcap int}
The structure of the go language is the same as the class, but again, it cannot be inherited and can have a combination (composition) attribute
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 ()}
Foo class, which inherits from the Base class and overwrites the Bar () method, and if the method is not overridden, calling Foo.bar () is the same as calling Foo.Base.bar ()
2. In go, you can modify the memory layout
Type Foo struct {...//other member base}
3. You can also "derive" from a type by pointer
Type Foo struct{*base ...}
The anonymous combination generally solves the virtual base class in C + +, which has the advantage that it can hide its combined class X without having to care about its implementation mechanism. But at the same time, the combined class X cannot access the member variables of that Class A, because a does not exist at the time of writing x, which is also part of the need to consider
Problem:
Variable name, workaround 1, variable hidden, 2, error
Initialization
In the go language, a variable that is not explicitly initialized is initialized to a value of 0 for that type , for example, a value of 0 of type bool of type False,int 0 value of 0,string type is an empty string of 0
Type Rect struct {x, y float64width, height float64}
Rect1:=new (Rect) rect2:=&rect{}rect3:=&rect{0, 0, 200}rect4:=&rect{width:100, height:200}
Because the go language has no constructors (the go language does not have an inheritance concept), the class is initialized with Newxxx as a constructor
Func newrect (x, y, width, height float64) *rect {return &rect{x, y, width, height}}
developers also don't need to analyze how much is going on behind the scenes after using new. In the go language, everything that's going to happen is directly visible.
Visibility of:
The symbol is defined to start with an uppercase letter, and the member can be accessed outside the package
If the lowercase letter starts, it can be accessed within the package
In summary, the accessibility of symbols in the Go language refers to packet-level, unrestricted access between classes under the same package, and if the type is the same as C + +, the access between classes and the class needs to be added to the keyword friend