This is a creation in Article, where the information may have evolved or changed.
Go object-oriented
There is no hidden this pointer in go
Type System
Type system is the type system of a language, and the Go type system contains the following basic content:
-basic types, such as Byte, int, bool, float, etc.
-composite types, such as arrays, structs, pointers, etc.
-Can point to any type of object
-Value semantics and referential semantics
-Object-oriented, that is, all types with object-oriented features
-Interface
The type system of the Go language:
-method to add a response to any type
IntegerIntegerLessInteger{ return a < b }
- Value semantics and referential semantics
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 arrays, structs, and pointers.
There is no difference between the array and the base type in go, and when you want to express a reference, you need to use the pointer
var a = [3]int{3,4,5}var b = &a
Several special types:
-Array slices: value semantics
-Map: Referential semantics
-Channel: Referential semantics
-Interface: With reference, internally maintained two pointers
type interface struct { data *void itab *Itab}
- struct: Supports only combination types
Initialization
Variables that are not initialized in the go language will be initialized to the 0 value of that type.
Anonymous combination
The way to inherit the go language is to use anonymous combinations.
type Base struct { ...... }type Foo struct { Base // Base的位置直接与内存布局相关 ...}// Foo类从Base继承并改写了Bar方法func (foo *Foo) Bar() { foo.Base.Bar() //实现时先调用基类Bar()方法}
There is also a way to inherit, but this approach requires an external pointer to a base class instance when creating an instance of Foo.
type Base struct { ...... }type Foo struct { *Base ...}
Visibility of
To make a symbol visible to other packages, you need to define the symbol as an uppercase letter
Interface
The interface is the cornerstone of the go language's entire type system, allowing the go language to reach unprecedented heights in the quest for basic programming philosophy. Interfaces in other languages are intrusive when used.
The Go Language interface uses non-intrusive.
typeFilestruct{ ... }func(f *file) Read (buf []byte) (nint, err Error)func(f *file) Write (buf []byte) (nint, err Error)typeIFileInterface{Read (buf []byte) (nint, err Error) Write (buf []byte) (nint, err Error)}typeIreaderInterface{Read (buf []byte) (nint, err Error)}typeIwriterInterface{Write (buf []byte) (nint, err Error)}varFile1 IFile =New(File)varFile2 Ireader =New(File)
The assignment of an interface is divided into two scenarios in the Go language:
-Assigning an object instance to an interface
This approach requires that the object implement all the methods required by the interface
type IntegerIntFunc (AInteger) Less (bInteger) BOOL {returnA<b//Go can automatically generate new methods based on the above method//func (a *integer) less (b Integer) bool {//Return a < b// }//However, cannot be reversed, becauseFunc (A*Integer) Add (bInteger) {*A+=b}typeLessadder interface {less (bInteger) BOOL Add (bInteger) }//If you want to assign an integer object to an interface, use only (1) because the *integer satisfies the Lessadder interfacevarAInteger = 1varb Lessadder= &A//(1)varb Lessadder=A//(2)
- Assigning an interface to another interface
Interface query:
...if file5, ok := file1.(two.IStream); ok { ...} ...switch v := v1.(type) { case int: // case string: // }
Interface combination:
type Reader interface { Read(p []byte) (n int, Err error) } type Writer interface { Write(p []byte) (n int , err error)} type readwriter interface { ReaderWriter } Equivalent to type readwriter interface { Read(p []byte) (n int
, err error) Write(p []byte) (n int, C16>err error)}
Any type: interface{}