Go start: 5, Composite type 2--pointer (pointer), struct (struct)

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

Before talking about the composite type of go to arrays and slices, today continue to look at pointers (pointer) and struct (struct).

Pointer (pointer)

Fetch address character –&

Go has pointers. The pointer holds the memory address of the variable.
As we all know, a variable is a handy placeholder for referencing a computer's memory address.
Here, let's say the address of the Go language – &, which is used before a variable, returns the memory address of the corresponding variable.

package mainimport"fmt"func main() {    varint = 20    varstring"a"    fmt.Printf("a 变量的存储地址: %x\n", &a)    fmt.Printf("s 变量的存储地址: %x\n", &s)}

%x is the hexadecimal output.

As you can see, in the result of two runs, the variable address of type int is different, and the variable address of string type is the same, is this the same as Java? –int is a value type and string is a reference type.

The definition of a pointer

Type *t is a pointer to a value of type T. Its 0 value is nil.

var p *int

The above defines a pointer of type int.
The & operator generates a pointer to its operand.
The * operator represents the underlying value pointed to by the pointer.

 PackageMainimport"FMT"Func main () {var p*intDefines a pointer variable FMT. Printf ("Initial storage address for P variable: %x\ n", p) I: =Tenp = &i//point pointer to variable i FMT. Printf ("Storage address for P variable: %x\ n", p)/* Use pointers to access values*/Fmt. Printf ("The value of the*p variable: %d\ n",*p) FMT. Printf (value of "I variable: %d\ n"I*p= -Set I FMT by Pointer p. Printf ("Storage address for P variable: %x\ n", p)/* Use pointers to access values*/Fmt. Printf ("The value of the*p variable: %d\ n",*p) FMT. Printf (value of "I variable: %d\ n", i)}


Can be seen,

    • The definition is the default address is 0, then its value is nil.
    • When the pointer points to the variable I, its address becomes the address of the variable I, completed by the & operator, then his value is the value of the variable i.
    • When the value of the pointer is changed by the operator *, the value of the corresponding I also changes, assuming that they are referencing a value on the same address.
      This is often referred to as an "indirect reference" or "redirect."
      To summarize, it takes three steps to use a pointer, first, to define a pointer variable. Then, assign a value (&) to the pointer variable. Finally, you can access the value (*) that points to the address in the pointer variable.

Pointer to pointers

If a pointer variable holds the address of another pointer variable, the pointer variable is called a pointer variable pointing to the pointer. When you define a pointer variable pointing to a pointer, the first pointer holds the address of the second pointer, and the second pointer holds the address of the variable.

The declaration format is as follows:

var ptr **int;

The pointer variable above points to the pointer is an integral type.

 PackageMainimport"FMT"Func main () {var aintvar ptr*intvar pptr**intA =Ten/* Pointer ptr address*/PTR = &a/* Pointing to the pointer ptr address*/Pptr = &ptr/* Gets the value of pptr*/Fmt. Printf ("Variable a = %d\ n", a) fmt. Printf ("Variable A memory address %x\ n", &a) fmt. Printf ("pointer variable *ptr = %d\ n",*ptr) FMT. Printf ("pointer variable *ptr memory address %x\ n", &*ptr) FMT. Printf (pointer variable pointing to pointer * *pptr = %d\ n ",**PPTR) fmt. Printf (pointer variable pointing to pointer * *pptr memory address %x\ n, &**PPTR)}


As you can see, the variable a,ptr,pptr all point to the same address. They actually refer to the same memory address, so their values are the same.
In short, the use of pointers in Go is still very extensive, and most of the same C language has some common points, but unlike C, Go does not have pointer operations.

struct (struct)

A struct (struct) is a collection of fields consisting of a series of data of the same type or different types. You can define different data types for different items in a struct.

Define and initialize

struct keys are used to define structs, and type declarations are defined types.

struct {    string// name of the object    valueint// its value}

The following code:

"fmt"type Vertex struct {    X int    Y string}func main() {    var v = Vertex{1"s"}    fmt.Println(v)    fmt.Println(v.X)    fmt.Println(v.Y)    v.X10    fmt.Println(v.X)}


The code above defines a struct named vertex, which consists of an int and a string of two data types.
A variable of type vertex is then defined in the main function. Through the above program can also be concluded that the operation of a structure of the content directly through . operator, whether it is a value or an assignment.

struct-Body pointer

A struct pointer is a pointer to a struct, similar to another pointer variable. Define struct pointers, and the fields of the struct can be accessed through struct pointers.
Special prefix & Returns a pointer to the struct body.
If we have a pointer to the struct p, then we can pass (*p). X visit its field x. But this is too verbose, so the language also allows us to use implicit indirect reference, directly write p.x.

package mainimport"fmt"typestruct {    int    string}func main() {    v := Vertex{1"2"}    p := &v    p.X = 333333333    fmt.Println(v)    fmt.Println(p.X)    fmt.Println((*p).X)}


As can be seen, the definition of pointer p to structure v, for P is modified to modify the previous structure of the body v.

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.