We have learned some basic knowledge about Golang, and it is clear that Golang is very similar to C, so when you see this, you may be even more shocked, this may be a new era of C language.

Pointer to the

Variables are convenient placeholders used to refer to computer memory addresses. The Go address is &, and when used before a variable, it returns the memory address of the variable.

Example code is as follows:

package main

import "fmt"

func main(a) {
   var a int = 10   

   fmt.Printf("Variable address: %x\n", &a  )
}
Copy the code

The output is as follows, perhaps different from one computer to another:

Variable address: C420014058

Copy the code

The concept of a pointer is: the memory address that points to any value, is the number of the memory address. Key points:

  • Is the basic data type in GO
  • Represents memory address
  • An address that can point to any data
  • Pointers to different data types require different combinations of data types and pointer symbols to represent them

Let’s look at some simple examples:

// Pointer declaration format
// var var_name *var-type
// var-type is the type of the pointer, var_name is the name of the pointer variable, and * is used to specify whether the variable is used as a pointer.

var ip *int        /* points to an integer */
var fp *float32    /* points to floating point */

Copy the code

Pointers, like any other type of variable, need to be declared and assigned before they can be used.

package main

import "fmt"

func main(a) {

	var a int = 20    /* Declare the actual variable */

	var aPointer *int /* Declare a pointer variable */

	aPointer = &a /* The storage address of the pointer variable */

	fmt.Printf("The address of variable A is: %x\n", &a)

	/* The storage address of the pointer variable */
	fmt.Printf("APointer variable stored pointer address: %x\n", aPointer)

	/* Use Pointers to access values */
	fmt.Printf("*aPointer variable value: %d\n", *aPointer)
}
Copy the code

The output is as follows (different computers may have different addresses) :

The address of variable A is: C420014050

APointer variable store pointer address: C420014050

*aPointer variable value: 20

Copy the code

In fact, for a more detailed use of Pointers, you can see the university C language textbooks, and data Structures C language edition.

Null pointer

When a pointer is defined without assigning any variables, its value is nil.

Nil Pointers are also called null Pointers.

Nil is conceptually the same as null, None, nil, and null in other languages.

A pointer variable is usually abbreviated to PTR.

Note that Pointers also have the following operations:

  • Pointer arrays (arrays and Pointers at heart)
  • Multilevel Pointers (Pointers to Pointers)
  • A pointer argument to a function

Let’s look at the following example:

package main

import "fmt"

func main(a) {

	var a int = 20 /* Declare the actual variable */

	var aPointer *int /* Declare a pointer variable */

	aPointer = &a /* The storage address of the pointer variable */

	fmt.Printf("The address of variable A is: %x\n", &a)

	/* The storage address of the pointer variable */
	fmt.Printf("APointer variable stored pointer address: %x\n", aPointer)

	/* Use Pointers to access values */
	fmt.Printf("*aPointer variable value: %d\n", *aPointer)

	b := 35
	bPointer := &b

	fmt.Printf("a = %d ,b = %d \n", a, b)
	fmt.Printf("aPointer = %x ,bPointer = %x \n", aPointer, bPointer)

	swap(aPointer, bPointer)

	fmt.Println("The result of the exchange.")
	fmt.Printf("a = %d ,b = %d \n", a, b)
	fmt.Printf("aPointer = %x ,bPointer = %x \n", aPointer, bPointer)

	pBPtr := &bPointer

	fmt.Printf("PBPtr = %x, *pBPtr = %x, **pBPtr = %x \n", pBPtr, *pBPtr, **pBPtr)

}

func swap(a, b *int) {
	tmp := *a

	*a = *b

	*b = tmp
}

Copy the code

The output is hi:

The address of variable A is: C420014050

APointer variable store pointer address: C420014050

*aPointer variable value: 20

A is 20, b is 35

APointer = C420014050, bPointer = C420014060

The result of the exchange

A is equal to 35, b is equal to 20

APointer = C420014050, bPointer = C420014060

PBPtr = c42000C030, *pBPtr = C420014060, **pBPtr = 14

Copy the code

In the above example, we can clearly see that the values of A and B change after the exchange, but the values of aPointer and bPointer do not change.

In the exchange function, we directly change the values of A and B through pointer manipulation, but aPointer and bPointer point to the addresses of A and B. We do not change them, so the values of aPointer and bPointer remain unchanged.

Then we declare and initialize a secondary pointer pBPtr that is the address of bPointer and *pBPtr that is the address of B.

The structure of the body

Structs, like the ones we encountered in C, are used to assemble disparate data (whether of the same type or not) together. In other object-oriented languages, such as Java, we are used to writing bean objects as follows:

Create the Person entity in Java
class Person implements Serializable{
    private String name;
    private Integer age;
}
Copy the code

So how do we do that in GO?

type Person struct {
	name string
	age  int
}
Copy the code

Of course, let’s take a look at the demo:

package main

import "fmt"

type Person struct {
	name string
	age  int
}

// Here is the method, enter the Person method
func (person Person) logPerson(a) {
	fmt.Printf("%s age: %d", person.name, person.age)
}

func main(a) {
	var aPerson Person

	aPerson.name = "The language"
	aPerson.age = 8
	aPerson.logPerson()

}
Copy the code

In the above code, we used some knowledge points:

  • Variables, declarations, and assignments
  • Package name, guide package, main function
  • Function, method
  • The structure of the body

In the main function, we declare the aPerson variable and the data type is the structure Person. We then initialize the specific properties of the structure and call the structure’s own methods to print information about Person.

Of course the structure has the following operations:

  • Property access, structure variable name. The property name
  • Use methods like basic data types (declare variables, function parameters, Pointers, and so on)

The following demo illustrates the general usage of structs:

package main

import "fmt"

type Person struct {
	name string
	age  int
}

func (person Person) logPerson(a) {
	fmt.Printf("%s, age: %d \n", person.name, person.age)
}

func printPersonByPointer(person *Person) {
	fmt.Printf("%s, age: %d \n", person.name, person.age)
}

func printPerson(person Person) {
	fmt.Printf("%s, age: %d \n", person.name, person.age)
}

func main(a) {
	var aPerson Person

	aPerson.name = "The language"
	aPerson.age = 8
	aPerson.logPerson()

	printPersonByPointer(&aPerson)
	
	printPerson(aPerson)
}

Copy the code

Note: Function overloading is not supported in the GO language

conclusion

Go and C have a lot in common. Pointers and structures are similar concepts in C.

  • Pointers support operations of any dataType and are declared in the following format: var *name dataType
  • The pointer stores the memory address, & is the address of the variable, and * is the value of the pointer to memory
  • The hierarchy of multilevel Pointers
  • Structs can be called extended data types or wrapper data types
  • Structs are used to define non-basic data types
  • Basic operations on the structure (property reads and writes, function parameters, structure Pointers)

If you recognize what I have done and think it is of some help to you, I hope you can also give me a cup of coffee, thank you.