Go language struct syntax

Source: Internet
Author: User

struct

-Define structure using type<name>struct{}, name follows visibility rules

-Supports pointer type members pointing to themselves

-Supports anonymous structures, action members, or defined member variables

-Anonymous structure can also be used for map values

-The structure can be initialized with literal values

-Allow values to read and write struct members through pointers

-members of the same type can make direct copy assignment

-Support = = with! = comparison operator, does not support > or <

-Supports anonymous fields, essentially a field defined with a type name

-Embedded structure as anonymous field looks like inheritance, but not inheritance

-Pointers for anonymous fields can be used

Package Main

Import "FMT"

Type Test struct{}


Func Main () {

A: =test{}

Fmt. Println (a)


}


Package Main
Import "FMT"
Type test struct {
Name string
Age int
Address string
}
Func Main () {
A: = test{}
A.name = "YH"//value initialization, similar to class in other languages, the go language does not have pointer arithmetic,
A.age = 18
Fmt. Printf ("My name is%s, this year's party%d\n", A.name, A.age)
B: = test{
Name: "YH",
Age:19,
Address: "Japan",
}
Fmt. Printf ("My name is%s, this year's party%d, I am%s\n", B.name, B.age, b.address)
}


Anonymous structure

Package Main
Import "FMT"
Type test struct {
Name string
Age int
Address string
Contact struct {
User_phont string
City string
}
}
Func Main () {
A: = Test{name: "YH", Age:19, Address: "Bj"}
A.contact.user_phont = "1234567890"
a.contact.city = "Haidian"
Fmt. Println (a)
}



Package Main

Import "FMT"

Func Main () {

A: = struct {

Name string

Age int

}{

Name: "AA",

Age:19,

}

Fmt. Println (a)

}


Package Main

Import "FMT"

Type Test struct{

Name string

Age int

}


Func Main () {

A: =test{

Name: "Coolqi",//Set literal value initialization

}

a.age=19//

Fmt. Println (a)


}



Package Main

Import "FMT"

Type Humen struct{

Sex int

}


Type Teacher struct{

Humen

Name string

Age int



}

Type Student struct{

Humen//The embedded is an anonymous field, essentially the name of the structure as the field name, if needed to initialize as a literal value, need to do humen:humen{sex:1}

Name string

Age int

}

Func Main () {

A: =teacher{name: "Joe", age:19,humen:humen{sex:1}}//in the embedded structure,

Fmt. Println (a)


}


This article is from the "Dbaspace" blog, make sure to keep this source http://dbaspace.blog.51cto.com/6873717/1963454

Go language struct syntax

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.