Akik Look at that coder

Public account: Look at that code farmer

The previous installment introduced the Go Language learning Interface | Go Theme month

  • What is an interface
  • The format of the interface declaration
  • Conditions for implementing interfaces

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

1. Introduction to this paper

The difference between Go language learning and learning

2. What is “&”

As we all know, in Go language variables will store their value in the random access memory of the computer, and the storage location of the value is the memory address of the variable. By using the address operator represented by &, the memory address of the specified variable can be obtained.

For example, in the following case:

package main

import (
   "fmt"
)

func main() {
   node: =10
   fmt.Printf("Node memory address: %v",&node)
}
Copy the code

The output is:

3. What is *?

As shown in the example above, the address operator & provides the memory address of the variable, while * provides the value to which the memory address points.

For example, in the following case:

package main

import (
   "fmt"
)

func main() {
   node: =10
   fmt.Printf("Node memory address: %v\n",&node)
   address:= &node
   fmt.Printf("Node value: %v",*address)
}
Copy the code

The output is:

At the same time, * is also used to indicate that a variable is of pointer type

For example, in the following case:

package main

import "fmt"

func main() {
   str: =new(string)
   *str = "Chongqing"
   fmt.Println(*str)
}
Copy the code

The output is:

4. Cases where “&” and “*” are used together

package main

import (
   "fmt"
)

func main() {
   // Prepare a string type
   var str = "Hello World"

   // Take the address of the string. P is of type *string
   p := &str

   Print the type of p
   fmt.Printf("P of type: %T\n", p)

   // Prints the pointer address of p
   fmt.Printf("The pointer to p is: %p\n", p)

   // Perform the value operation on the pointer
   value := *p

   // The specified type
   fmt.Printf("Value of type: %T\n", value)

   // The value of the pointer points to the variable
   fmt.Printf("The value of the variable is: %v\n", value)
}
Copy the code

The output is:

5. To summarize

To sum up, we can draw a conclusion:

  • & Is to take the address symbol, that is:Gets the address of a variable, such as:&a.
  • *Is a pointer operator that can representOne variable is a pointer typeCan also representThe storage unit to which a pointer variable points, which is the value stored at this address.

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.