Go language-pointer

Pointer

I'm going to write the understanding of Go from the basics of programming!

What is a pointer?

Address is a value Can be manipulated like an integer or string or assigned to a variable Go treats addresses as pointers A variable to which a pointer is assigned is called a pointer type variable.

Definition of pointer type variable

func main() {
  name := "tanabe"  //Because the value of name is string type*Become a string type

  var namePtr *string = &name
  fmt.Println(namePtr)

//console
0xc421100230  //Pointer outputs

To define a pointer type variable Declare the data type of the variable with "* (asterisk)"

The pointer type variable namePtr is defined and the pointer obtained by & name is assigned.

Access from a pointer

func main() {
  name := "tanabe"

  var namePtr *string = &name
  fmt.Println(*namePtr)  //Output the value of the variable indicated by namePtr
}

//console
tanabe  //The value is output

To define a pointer type variable Declare the data type of the variable with "* (asterisk)"

Update the value using a pointer

func main() {
  name := "tanabe"
  var namePtr *string = &name
  *namePtr = "naito"
  fmt.Println(name)  //Output the value of the variable indicated by namePtr
}

//console
naito  //The updated value is output

You can also use pointer variables to update values ​​directly To update, write "* variable name = value to be updated"

Update variable name with "* namePtr =" naito ""

Specify a pointer as an argument

func main() {
  name := "tanabe"
  changeName(&name)  //Specify a pointer as an argument
}

func changeName(namePtr *string) {  //Receive as a pointer type of string
}

When specifying a pointer as an argument to another function It is necessary to prepare the corresponding pointer type argument for the function to receive

Update using a pointer in another function

func main() {
  name := "tanabe"
  changeName(&name)  //Specify a pointer as an argument
  fmt.Println(name)
}
func changeName(namePtr *string) {  //Receive as a pointer type of string
  *namePtr = "naito"
}

//console
naito

By specifying a pointer as an argument You can use pointers to update the original variable from another function

Specify a variable as an argument

func main() {
  totalScore := 0
  fn(totalScore)
...
}
func fn(totalScore int) {
...
}

When the variable totalScore is specified as an argument The variable itself is not passed

The value of the variable totalScore is copied and assigned to a new variable (passed by value)

Update the passed value

func main() {
  totalScore := 0
  fn(totalScore)
  fmt.Println(totalScore)  //Output the value of totalScore
...
}
func fn(totalScore int) {
  totalScore += 10  //Update value only for totalScore of fn function
}
//console
0  //The original value is output

Each variable totalScore of the main function and fn function is a different variable Therefore, even if you update the totalScore of the fn function Only the totalScore of the fn function is updated

Compare pointers

func main() {               func fn(totalScore int) {
  totalScore := 0             totalScore += 10
❶fn(totalScore)
❷fmt.Println(totalScore)     ❶fmt.Println(totalScore)
  fmt.Println(&totalScore)     fmt.Println(&totalScore)
}                           }

//console
❶ TotalScore value and pointer of fn function
10  //The original value is output
0xc421100230

❷ value and pointer of totalScore of main function
0
0xc421100052

If you output the totalScore of each of the main function and fn function It turns out that they are actually variables that are elsewhere

Argument summary

When a value such as an integer is specified as an argument

func main() {
  a := 10
  calculate(a)
  fmt.Println("When an integer is specified as an argument:", a)
}
func calculate(a int) {  //Receive values ​​such as integers
  a += 1
}

//console
When an integer is specified as an argument:10  //The variable a of the main function has a different scope, so it remains as it is

The calculate function is a function that adds 1 to the received argument

If variable a is specified as an argument, the value will be copied and another variable will be generated. Therefore, even if the value is updated by the calculate function, the variable a of the main function is not updated.

When a pointer is specified as an argument

func main() {
  b := 10
  calculate(&b)  //Pass a pointer
  fmt.Println("When a pointer is specified as an argument:", b)
}
func calculate(bPtr*int)  //Get a pointer
  *bPtr
}
//console
When an integer is specified as an argument:11  //The value of variable b of the main function is updated

If you specify a pointer as an argument, you can update the value of the original variable from the calculate function. At this time, note that the argument to be received specifies the pointer type.

Recommended Posts

Go language-pointer
Go Language-Basic ❶
Go language-fmt.Scan
Go language-address
Go Language-Functions
Go Language-Basic ❷
GO Chokisomemo 1
Go language-iteration
Python with Go
Go language-conditional branching
About Go functions
Go language-standard package
Go class basics
Go language-rand package
About Go Interface
use go module