[Go language] learn go--pointers and structs from Docker source

Source: Internet
Author: User

These two days in looking at reflect this package in the use of Docker, encountered a variety of problems, finally although know how to use.

But the principle of the piece is not too understanding, so the "way to GO" in the key chapters to see the next.

Keep on writing and strive to be able to speak clearly.

Source

Or first look at the source in the Docker, Docker/api/client/cli.go

Type DOCKERCLIstruct{ProtostringAddrstringConfigFile*Registry. ConfigFileinchio. Readcloser outio. Writer err io. Writer isterminalBOOLTERMINALFD uintptr tlsconfig*TLS. Config Schemestring}...func newdockercli (inchIo. Readcloser, out, err Io. Writer, Proto, addrstring, Tlsconfig *tls. Config) *dockercli {...return&dockercli{Proto:proto, addr:addr,inch:inch,         out: out, Err:err, Isterminal:isterminal, TERMINALFD:TERMINALFD, Tlsconfig:tlsconfig, Scheme:scheme,}}

A struct is defined and an initialized method is defined (the method internally initializes a struct and returns its address.) )

Pointer

Let's start by introducing the pointers in Go

We define a variable in the program, it is allocated a space in memory, the value stored in this space is the value of the variable, and the address of this space can be assigned to a pointer.

The pointer is stored in the memory address of a variable, not the actual variable value.

Declare a pointer in the following way (type preceded with * symbol)

var intp *int

Assigning values to pointers & symbols

1= &i1

By adding the * symbol before the pointer variable, you can get the value of the pointer pointing to the variable.

Int1 = = * (&int1)

Use pointers as much as possible during the pass-through process, which can greatly reduce memory costs and increase the speed of the transfer of values.

One drawback, however, is that when you get a value, you have a bit more of a relationship, so it's slightly slower in this part of the performance.

struct use

struct format

struct {    field1 type1    field2 type2    ...}

struct initialization, see the following example of the 3 cases to understand.

//MainPackage Mainimport"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="Lemon"Pers1.lastname="Bar"Upperson (&pers1)//2-struct as a pointer:Pers2: =New(person) pers2.firstname="Lemon"Pers2.lastname="Bar"    //(*pers2). LastName = "Bar"//This is also validUpperson (pers2)//3-struct as a literal:PERS3: = &person{"Lemon","Bar"}    //Pers3: = &person{firstname: "Lemon", LastName: "Bar"}//This is also validUpperson (PERS3)}

Note: New comes with a pointer

We can initialize it in the form of a factory method, in addition to being directly initialized.

Func newdockercli (in Outstring, Tlsconfig *tls. Config) *dockercli {    ...}

Note: If you want this struct to belong to the package, can only be initialized by the factory method, you can change the first letter of the struct name to lowercase, that is, the private, the package can only be initialized by the factory method.

Anonymous fields

Anonymous fields can implement inherited functions in other languages.

Package MainType innersstruct{in1intin2int}type Outersstruct{bintC float32int    //Anonymous FieldInners//Anonymous Field}func Main () {outer:=New(outers) outer.in1=3 //Outers.in1 is inners.in1Outer.in2 =4 //outers.in2 is inners.in2Outer.int=5 //int is also the anonymous filed nameOuter2:= outers{5,6.7, -, inners{3,4}}//Outers can also is initialized in this format}

The filed inside the anonymous struct becomes the field of the outside struct.

Note: Within a struct, there can be only one anonymous type of field for each type.

Name Conflict issues

Since a struct can contain multiple anonymous, different types of structs, what does go do when there are filed with the same name?

1. The outer field has a higher priority than the inner field, which means that the outside will overwrite it.

2. If two field levels are the same

A. If the field name is not used, there will be no problem.

B. If it is used, there will be a compile error.

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.