I'm going to write the understanding of Go from the basics of programming!
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.
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.
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)"
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 ""
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
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
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)
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
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
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.
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