Structure and method of Golang notes

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

GolangIs struct a bit similar to the Java class in.

struct

Whatever the characteristics of the character, the first paragraph of code. See what the difference is with other languages you've learned.

package mainimport (    "fmt")type Person struct {    name string    age  int}type Student struct {    grade  string    Person //匿名字段}func main() {    stu := new(Student)    var stu2 Student = Student{"一年级", Person{"Arya Stark", 15}}    stu3 := Student{grade: "二年级", Person: Person{"Magicer", 32}}    stu.grade = "三年级"    stu.Person = Person{"Jon Snow", 33}    fmt.Println(stu) //输出  &{三年级 {Jon Snow 33}}    //在这里可以直接stu.name获得匿名字段的值,也可以stu.Person.name    fmt.Println(stu.name, stu.age, stu.grade, stu.Person) //输出Jon Snow 33 三年级 {Jon Snow 33}    fmt.Println(stu2)                                     //输出   {一年级 {Arya Stark 15}}    fmt.Println(stu3)                                     //输出   {二年级 {Magicer 32}}}

GolangThere is no inheritance in it, but we can use a combination of methods to implement and inherit similar effects. For example, the above code. We can't do this by showing the Student inheritance, Person but we can get this effect by combining it.

Type

We can define a type by using the Type keyword. For example, the Person type and Student type here. We can also use type keywords to int set aliases such as type grade int . defined by the Type keyword

Method

First of all, the code about the method, look at Golang the method is a ghost.

package mainimport (    "fmt")type Student struct {    name string    age  int}//值方法//当我们不需要在方法中使用这个结构的值的时候, 可以使用_func (_ Student) lean() {    fmt.Println("Student lean")} //指针方法//这里的(stu *Student)用来表示这个方法是属于谁的.可以位于不同的文件中,只要是在同一个包中就可以//在这里我们的stu是Student的指针,只有这时候我们才可以修改p的值,// 当使用(stu Student)时,我们使用的是一份copy.func (stu *Student) eat(food string) {    fmt.Println(stu.name, " eat ", food, "\t ", stu)}func main() {    stu := new(Student)    stu.eat("Apple") //student eat  Apple     &{ 0}    stu.lean()       // Student lean    var stu2 Student = Student{"Jason", 22}    fmt.Println(stu2) //{Jason 22}    stu2.eat("Apple") //Jason  eat  Apple     &{Jason 22}}

We see that Student the name and age fields above are non-export fields. So what if we were going to modify the values in different packages?
This is when we can write the Java setter getter following methods.

func (stu *Student) setName(name string) {    stu.name = name}func (stu *Student) getName() string {    return stu.name}

A Golang beginner, the record of things inevitably have errors, but also hope that many points ah. Thank you.

Related Article

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.