Golang Learning Notes (vi): Struct_golang

Source: Internet
Author: User

struct

struct, a set of fields, similar to the class in other languages

A large number of object-oriented features, including inheritance, have been discarded, leaving only the most fundamental features of the composition (composition)

1. Declaration and initialization

Copy Code code as follows:

Type person struct {
Name string
Age int
}

Class

Func Main () {
var P person

P.name = "Tom"
P.age = 25
Fmt. Println (P.name)

P1: = person{"Tom1", 25}
Fmt. Println (P1.name)

P2: = person{age:24, Name: "Tom"}
Fmt. Println (P2.name)
}

2.struct anonymous field (inheritance)

Copy Code code as follows:

Type Human struct {
Name string
Age int
Weight int
}

Tyep Student struct {
Human//anonymous field, default student contains all fields Human
Speciality string
}

Mark: = Student (human{"Mark", "a", "Computer Science")

Mark.name
Mark.age


The ability to implement field inheritance, when the field name is repeated, priority to take the outer, you can specify the struct name also decide which to take
Copy Code code as follows:

Mark. Human = human{"A", 55, 220}
Mark. Human.age-= 1

struct not only can you use struct as an anonymous field, a custom type, a built-in type can be an anonymous field, and you can do a function on the corresponding field

3.method

Copy Code code as follows:

Type Rect struct {
X, y float64
width, height float64
}

Method


Reciver is passed by default, not by reference, and can be a pointer
The pointer acts as receiver on the contents of the instance object, whereas the normal type acts as a receiver only as a copy of the action object, not the original instance object
Copy Code code as follows:

Func (R recivertype) FuncName (params) (results) {

}


If the receiver of a method is *t, it is possible to pass an instance variable V of type T without using &v to invoke this method
Copy Code code as follows:

Func (R *rect) area () float64 {
Return r.width * r.height
}

Func (b *box) SetColor (c Color) {
B.color = C
}

4.method inheritance and overrides

Implement inheritance in a combinatorial way

Copy Code code as follows:

Type Human struct {
Name string
}

Type Student struct {
Human
School string
}

Func (H *human) Sayhi () {
Fmt. Println (H.name)
}

Then instances of student and employee can call the
Func Main () {
H: = human{name: "Human"}
Fmt. Print (H.name)
H.sayhi ()

S: = student{human{"Student"}}
S.sayhi ()

}


You can also do a method override
Copy Code code as follows:

Funct (e *student) Sayhi () {
E.human.sayhi ()
Fmt. Println (E.school)
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.