This is a creation in Article, where the information may have evolved or changed.
The structs in go can implement classes and methods in OOP. Struct members in the go language can be any type, such as normal type, compound type, function, struct, interface, and so on.
Entry
//DefinitionType Userstruct{NamestringEmailstringPasswordstring}//Statement varUser UservarUserref *user FMT. PRINTLN (user)//{} default initialized to 0 valueFmt. Println (USERREF)//<nil> not initializedUserref =New(User) fmt. Println (USERREF)//&{} //Initializeuser = User{name:"Liming", Email:"Liming@gamil.com", Password:"pw123456"} userref = &user{name:"Liming", Email:"Liming@gamil.com", Password:"pw123456"} FMT. PRINTLN (user)//{liming liming@gamil.com pw123456}Fmt. Println (USERREF)//&{liming liming@gamil.com pw123456} //Simple notationUser1: = User{name:"Liming", Email:"Liming@gamil.com", Password:"pw123456"} USERREF1: = &user{name:"Liming", Email:"Liming@gamil.com", Password:"pw123456"} FMT. Println (User1)//{liming liming@gamil.com pw123456}Fmt. Println (USERREF1)//&{liming liming@gamil.com pw123456} //reference differs from valueUpuser (user1) upuser (USERREF1)//cannot Use USERREF1 (type *user) as type User in argument to UpuserUpuserref (USERREF1) fmt. Printf ("Name is%s,%s", User1.name, Userref1.name)//name is liming, Liming //recursive structType Linkednodestruct{DatastringSu *linkednode} type Doublelinkednodestruct{PR *doublelinkednode datastringSu *doublelinkednode} type TreeNodestruct{Le *treenode datastringRI *treenode}
Advanced
Construction method
OOP, New Object (), go through factory method Newstructname
//Do not force the use of constructors, capitalize the first letterType Filestruct{FDintFileNamestring}func NewFile(fd int, name string) *file{ifFD <0{returnNIL}return&FILE{FD, name}}//Mandatory use of constructors, first letter lowercaseType File1struct{FDintFileNamestring}func NewFile1(fd int, name string) *file1{ifFD <0{returnNIL}return&FILE1{FD, name}}
Tag
You can add descriptions to the struct fields, which can be obtained by reflection
struct// tagsboolstringint “How much there are”}
Anonymous domain
There can be only one anonymous domain for each type. Can be used to implement inheritance in OOP
type anonymousstruct struct {name string int string File} anonymous: = new (anonymousstruct) anonymous.name = " Hanmeimei " Anonymous. int = 88 Anonymous. string = "中文版" Anonymous. FILE.FD = 10 //or ANONYMOUS.FD = ten A Nonymous. File.filename = "Xxoo.avi" //or anonymous.filename = " Xxoo.avi " FMT. Println (anonymous) //&{hanmeimei 中文 {Xxoo.avi}}
Method
Oop in the go language is very different, and the class in Go is called receiver,receiver, which can be any type except interface. Methods and classes are not organized together, traditional OOP methods and classes are placed in a file, and the go language can be scattered in different files as long as it is in the same package. Go's philosophy is data and implementation separation
Methods is not mixed with the data definition (the structs): they is orthogonal to types; Representation (data) and behavior (methods) are independent
The Go Method definition format is as follows
func (recv receiver_type) methodName(parameter_list) (return_value_list) { … }func (_ receiver_type) methodName(parameter_list) (return_value_list) { … }func (this receiver_type) methodName(parameter_list) (return_value_list) { … }func (self receiver_type) methodName(parameter_list) (return_value_list) { … }
OOP calls Object.Method, go calls recv. Method ()
Define non-struct type methods
type IntVector []intfunc (v IntVector) Sum() (s int) { for _, x := range v { s += x } return}
Method scope
Method and receiver must be defined in the same package.
import “container/list”//cannot define new methods on non-local type list.Listfunc (p *list.List)Iter() {// …}
Hack method
//hackstruct { list.List//anonymous field}func (p *myList)Iter() { // …}
Reciever
Reciever is best defined as a pointer form. A reciever that is not a pointer is automatically converted to a pointer form. Such as
func (u *User)Iter() { // …}u:=User{"liming",22}u.Iter//会转换成&User
Getter Setter
Personstruct { firstName string lastName string}func (p *Person)FirstNamereturn p.firstName}func (p *Person)SetFirstName(newName string) { p.firstName = newName}
Wen/mrkai the Ordinary Way (Jane book author)
Original link: http://www.jianshu.com/p/7b523f8ab141
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".