Golang Study of the struct

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

The general way to define a struct is as follows:

struct { field1 type1 field2 type2 ... }

type T struct {a, b int}It is also a legal grammar, which is more suitable for simple structures.

var t *T t new(T)

A variable t is a T pointer to a struct field whose value is the 0 value of the type they belong to, and uses the new function to allocate memory to a newer struct variable, which returns a pointer to the allocated memory.

Regardless of whether the variable is a struct type or a struct type pointer, the same selector character (selector-notation) is used to refer to the field of the struct, i.e.:

structint } var v myStruct    // v是结构体类型变量var p *myStruct   // p是指向一个结构体类型变量的指针v.i p.i

struct instance:

Package main Import ("FMT"    "Strings") Type personstruct{firstNamestringLastNamestring} func Upperson (P*Person ) {P.firstname=strings. ToUpper (p.firstname) p.lastname=strings. ToUpper (P.lastname)} func main () {//1-struct as a value type:    varpers1 person Pers1.firstname="Chris"Pers1.lastname="Woodward"Upperson (&pers1) fmt. Printf ("The name of the person is %s%s\n", Pers1.firstname, Pers1.lastname)//2-struct as a pointer:Pers2: =New(person) pers2.firstname="Chris"Pers2.lastname="Woodward" (*PERS2). LastName ="Woodward" //this is legal.Upperson (pers2) fmt. Printf ("The name of the person is %s%s\n", Pers2.firstname, Pers2.lastname)//3-struct as a literal:PERS3: = &person{"Chris","Woodward"} upperson (PERS3) fmt. Printf ("The name of the person is %s%s\n", Pers3.firstname, Pers3.lastname)}

Program output:

is CHRIS WOODWARD The name of the person is CHRIS WOODWARD The name of the person is CHRIS WOODWARD

Use reflection to access the tag tag of a struct:

Package main Import ("FMT"    "reflect") Type personstruct{firstNamestring' An important answer ' LastNamestring' The name of the Thing '} func main () {TT:= person{" First"," Last"}  forI: =0; I <2; i++{Reftag (TT, i)}}

Program output:

Tag:An important answer Name:firstName Tag:The name of the thing Name:lastName

To invoke a method on a value and a pointer:

You can have methods that are connected to a type, or you can have a method that connects to a type pointer.

But it doesn't matter: for type T, if there is a method on *t Meth() and it t is a variable of this type, it t.Meth() will be automatically converted to (&t).Meth() .

Both the pointer method and the value method can be called on a pointer or non-pointer, the type of the recipient is a *TwoInts method AddThem() that can be called on the value of the type TwoInts , which is automatically indirectly occurring.

two2.AddThemcan therefore be substituted (&two2).AddThem() .

Again, there are methods to inherit and overwrite in go, but it must be an anonymous field O

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.