Akik Look at that coder

Public account: Look at that code farmer

The previous installment introduced the data types of Go language learning | the five data types commonly used in Go theme month.

  • The integer
  • floating-point
  • The Boolean
  • string
  • slice

This article will continue to take you into the world of Go.

1. Introduction to this paper

This article will introduce

Pointers to the Go language

2. Understanding Pointers for the first time

The pointer concept is split into two concepts in Go.

  • Type pointer: Allows data of this pointer type to be modified. Pass data using Pointers instead of copying data, type Pointers cannot be offset or evaluated, and type Pointers cannot be offset or evaluated.

  • Slice: Consists of the original pointer to the starting element, the number of elements, and the container.

3. Pointer address and pointer type

To understand Pointers, you need to know a few concepts: pointer address, pointer type, and pointer value.

Each variable has an address at run time that represents its location in memory.

Go uses the ampersand operator in front of the variable to “fetch the address” in the following format:

str :=&v  // the pointer to v is of type TWhere v represents the variable of the address to be taken, and v of the address to be taken is received using STR variable. STR is of type "*T", which is called the pointer type of V, and "*" represents the pointerCopy the code

Are you confused? Let’s use a case to illustrate

package main
import (
   "fmt"
)
func main(){
var a int=1
var str string="apple"
fmt.Printf("%p,%p",&a,&str)
}
Copy the code

The output is

So in this case, we first declare the integer a variable, the string STR variable,

Using the “%p” of FMT.Printf, print the pointer to the address of the a and STR variables as a hexadecimal prefix with “0x”

The relationship between variables, Pointers, and addresses is that each variable has an address, and the value of the pointer is the address

4. Get the value from the pointer

A pointer to a normal variable can be obtained by using the ampersand operator to retrieve the address.

Pointers can be operated on with an “*” operation, that is, the value of a pointer

Take the following example

package main
import "fmt"
func main(){
   // Prepare a variable of type string
   var a ="Hello World!"
   // Take the address of the string, STR of type *string
   str := &a
   // Prints the type of STR
   fmt.Printf("str type: %T\n",str)
   // Prints the pointer address of STR
   fmt.Printf("address: %p\n",str)
   // Perform the value operation on the pointer
   value:=*str
   // The specified type
   fmt.Printf("value type: %T\n",value)
   // The value of the pointer points to the variable
   fmt.Printf("values: %s\n",value)
}
Copy the code

The output is

In the pointer

The address-fetch operator & and the value operator * are complementary operators. Ampersand fetches the address, and * fetches the value pointed to by the address.Copy the code

There are several important relationships that need to be made clear

  • To obtain a pointer to a variable, perform an address fetch (&) operation
  • The value of a pointer variable is a pointer address
  • The value (*) operation on a pointer variable can obtain the value of the original variable pointed to by the pointer variable

5. Use Pointers to change the value

Pointers allow you to not only take values, but also modify them.

We use a case to illustrate the process of using Pointers to exchange values.

package main
import "fmt"
// Define the exchange function
func swap(a,b *int){
   // Take the value of a pointer and assign it to the temporary variable t
   t:=*a
   // Assign the value of pointer B to the variable pointed to by pointer A
   *a=*b
   // Assign the value of pointer A to the variable pointed to by pointer B
   *b=t
}
// Write the main function
func main(){
   // Prepare two variables, assign 1 and 2
   x,y: =1.2
   // Swap variable values
   swap(&x,&y)
   // Outputs variable values
   fmt.Println(x,y) 
}
Copy the code

The output is

By defining the form of a new function, it is one of the ways to exchange pointer values

6. Another way to create Pointers – the New() function

In addition to the normal pointer creation operations described above, we can also create a new pointer with the following format:

New (type)

You can generally define it this way

package main
import "fmt"
func main(){

str: =new(string)
*str="Hello World!"

fmt.Printf("%s\n",*str)
fmt.Println(*str)
}
Copy the code

The output is

If you find this helpful:

1. Click “like” to support it, so that more people can see this article

2, pay attention to the public number: look at that code farmers, we study together and progress together.

This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign