Extracting fields and methods in the Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. A struct is a series of fields that contain names and types. Usually like this: "' Gopackage mainimport" fmt "type person struct {name Stringage int32}func main () {person: = Person{name:" Michał ", Age:29}fmt. PRINTLN (person)//{michał29}} "(In the next part of this post, I will progressively remove the package name, import, and definition of the main function) in the struct above, each with a clear name. The Go language also allows you to specify no field names. Fields that do not have a name are called anonymous fields or inline fields. The name of the type (if there is a package name, the package name that does not contain the prefix) is the name of the field. Because the struct requires at least one unique field name, we cannot do this: "Goimport (" net/http ") type request Struct{}type T struct {http. Request/Field name is "request" request//Field name is "request"} "if compiled, the compiler will throw an error:" ' > Go install Github.com/mlowic ki/sandbox# github.com/mlowicki/sandboxsrc/github.com/mlowicki/sandbox/sandbox.go:34:duplicate Field Request " With an anonymous field or method, you can access it in a concise way: ' gotype person struct {name Stringage int32}func (P-person) Isadult () bool {return p.age >= 18}type Employee struct {position String}func (E employee) Ismanager () bool {return e.position = = "Manager"}type Record s truct {personemployee}...record: = Record{}record.name = "Michał" Record.age = 29record.position = "Software Engineer" FMT. PRINTLN (record)//{{michał29} {software engineer}}fmt. PRINTLN (Record.name)//MICHAŁFMT.PRINTLN (Record.age)//29FMT. PRINTLN (record.position)//software engineerfmt. Println (record. Isadult ())//TRUEFMT. Println (record. Ismanager ())//False ' anonymous ' (embedded) fields and methods are automatically raised (to find the corresponding object) when called. They behave like regular fields, but cannot be used in struct literals: ' ' Go//record: = Record{}record: = record{name: ' Michał ', ' age:29} ', which causes the compiler to throw an error: ' '//src/ Github.com/mlowicki/sandbox/sandbox.go:23:unknown Record field ' name ' in struct literal//src/github.com/mlowicki/ Sandbox/sandbox.go:23:unknown Record field ' age ' in struct literal ' can achieve our purpose by creating a clear, complete, embedded structure: ' ' gorecord{ Person{name: "Michał", age:29}, Employee{position: "Software Engineer"}} "[Source:] (https://golang.org/ref/spec#Struct _types)! [] (https://raw.githubusercontent.com/studygolang/gctt-images/master/promoted-fields-and-methods/ promoted-fields-and-methods-in-go-1.jpg)

Via:https://medium.com/golangspec/promoted-fields-and-methods-in-go-4e8d7aefb3e3

Author: Michałłowicki Translator: gogeof proofreading: polaris1119

This article by GCTT original compilation, go language Chinese network honor launches

This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove

494 Reads

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.