Function pointers in Go
A pointer in Go is a special-purpose variable that stores the memory address of other variables and the point where the memory is located. It can also access the value stored in that memory location.
Pointer declaration in Go
var pointer_name *Data_Type
Operators used in function pointers
-
-
- It is a dereference operator that declares a pointer variable. You can use it to access the value stored in the dereferenced address.
-
- & − This is an address operator and is used to give the address of a variable.
Initializing a pointer in Go
package main
import "fmt"
func main() {
var myVariable int = 2
var myPointer *int
myPointer = &myVariable
fmt.Println("The value stored in 'myVariable' = ", myVariable)
fmt.Println("The address of 'myVariable' = ", &myVariable)
fmt.Println("The value stored in 'myPointer' = ", myPointer)
fmt.Println("The value pointed by 'myPointer' after dereferencing = ", *myPointer)
}
Output:
The value stored in 'myVariable' = 2
The address of 'myVariable' = 0xc000090020
The value stored in 'myPointer' = 0xc000090020.
The value pointed by 'myPointer' after dereferencing = 2
Note that the value stored in the myPointer variable is the address of the variable myVariable.
Using uninitialized pointers in Go
package main
import "fmt"
func main() {
var myPointer *int
fmt.Println("The value stored in 'myPointer' = ", myPointer)
}
Output:
The value stored in 'myPointer' = <nil>
请注意
, uninitialized pointers will have the default value of nil.
:=
Using shorthand syntax
for pointers in Go
package main
import "fmt"
func main() {
myVariable := 2
myPointer := &myVariable
fmt.Println("The value stored in 'myVariable' = ", myVariable)
fmt.Println("The address of 'myVariable' = ", &myVariable)
fmt.Println("The value stored in 'myPointer' = ", myPointer)
fmt.Println("The value pointed by 'myPointer' after dereferencing = ", *myPointer)
}
Output:
The value stored in 'myVariable' = 2
The address of 'myVariable' = 0xc000090020
The value stored in 'myPointer' = 0xc000090020
The value pointed by 'myPointer' after dereferencing = 2
请注意
, if you have used a pointer to declare a specified data type, you can only use the pointer to address the memory address of a variable of that specified data type.
:=
The operator automatically recognizes the data type and ensures that the created pointer can handle the same data type.
Changing the value of a variable using pointers in Go
package main
import "fmt"
func main() {
myVariable := 2
myPointer := &myVariable
fmt.Println("The value stored in 'myVariable' = ", myVariable)
fmt.Println("The address of 'myVariable' = ", &myVariable)
fmt.Println("The value stored in 'myPointer' = ", myPointer)
fmt.Println("The value pointed by 'myPointer' after dereferencing = ", *myPointer)
*myPointer = 5
fmt.Println("The updated value pointed by 'myPointer' after dereferencing = ", *myPointer)
fmt.Println("The updated value stored in 'myVariable' = ", myVariable)
}
Output:
The value stored in 'myVariable' = 2
The address of 'myVariable' = 0xc000016058
The value stored in 'myPointer' = 0xc000016058
The value pointed by 'myPointer' after dereferencing = 2
The updated value pointed by 'myPointer' after dereferencing = 5
The updated value stored in 'myVariable' = 5
Note that you can change the value of a pointer instead of assigning a new value to a variable. The value changed using the pointer is also reflected in the original value.
Creating pointers in Go using new() function
package main
import "fmt"
func main() {
var myPointer = new(int)
*myPointer = 2
fmt.Println("The value stored in 'myPointer' = ", myPointer)
fmt.Println("The value pointed by 'myPointer' after dereferencing = ", *myPointer)
}
Output:
The value stored in 'myPointer' = 0xc000016058
The value pointed by 'myPointer' after dereferencing = 2
请注意
, usingnew()
the function, you can directly create a pointer to an integer.
Passing pointers as arguments to functions in Go
package main
import "fmt"
func myFunction(myVariable *int) {
*myVariable = 5
}
func main() {
var myVariable = 2
fmt.Printf("The value of myVariable before function call is: %d\n", myVariable)
var myPointer *int = &myVariable
myFunction(myPointer)
fmt.Printf("The value of myVariable after function call is: %d\n", myVariable)
}
Output:
The value of myVariable before function call is: 2
The value of myVariable after function call is: 5
Notice that the function created above changes the value of the variable myVariable. The value changed inside the function is reflected outside the function because the pointer directly refers to the memory location where the variable is stored.
Passing the address of a variable as an argument to a function in Go
package main
import "fmt"
func myFunction(myVariable *int) {
*myVariable = 5
}
func main() {
var myVariable = 2
fmt.Printf("The value of myVariable before function call is: %d\n", myVariable)
myFunction(&myVariable)
fmt.Printf("The value of myVariable after function call is: %d\n", myVariable)
}
Output:
The value of myVariable before function call is: 2
The value of myVariable after function call is: 5
请注意
, the function created above changes the value of the variable myVariable. The value changed inside the function is reflected outside the function.
The changes are made to the memory location where the variable is stored. This is another way of passing a pointer to a function.
Returning a pointer from a function in Go
package main
import "fmt"
func main() {
displayText := display()
fmt.Println("Hello,", *displayText)
}
func display() *string {
myMessage := "World!"
return &myMessage
}
Output:
Hello, World!
请注意
, the function created above will return a pointer to the string myMessage. This value is then dereferenced in the main function.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Install GoLang using Brew
Publish Date:2025/04/15 Views:82 Category:Go
-
This article describes how to install GoLang using Brew on Linux or macOS. Install GoLang using Brew brew installs missing packages in Linux and macOS. It makes it easy to install GoLang on Linux or macOS. Follow the steps below to install
GoLang RWMutex Detailed Introduction
Publish Date:2025/04/15 Views:116 Category:Go
-
This article introduces how to use rwmutex in Go language. Go language RWMutex mutex is the abbreviation of mutual exclusion, which is used to keep track of which thread has accessed a variable at any time. Mutex is a data structure provide
Enabling CORS in GoLang
Publish Date:2025/04/15 Views:102 Category:Go
-
This article describes how to enable and use CORS in GoLang. Go language CORS Cross-origin resource sharing (CORS) is a process based on HTTP headers that defines the origins from which browsers are allowed to load and use resources. CORS i
Getting a string representation of a structure in Go
Publish Date:2025/04/15 Views:63 Category:Go
-
Go allows us to serialize data from structures using a variety of simple standard methods. Converting a structure to a string using String method in Go The GoLang package String helps implement simple functions to manipulate and edit UTF-8
Convert JSON to struct in Go
Publish Date:2025/04/15 Views:126 Category:Go
-
This article describes how to convert JSON to struct in GoLang . Convert JSON to Struct using Unmarshal method in Go The encoding/json package of the Go language provides a function Unmarshal to convert JSON data into byte format. This func
在 Golang 中使用 If-Else 和 Switch Loop Inside HTML 模板
Publish Date:2023/04/27 Views:104 Category:Go
-
本篇文章介绍了在 Golang 的 HTML 模板中使用 if-else 和 switch 循环。因此,只要输出是 HTML,就应该始终使用 HTML 模板包而不是文本模板。
Golang 中的零值 Nil
Publish Date:2023/04/27 Views:185 Category:Go
-
本篇文章介绍 nil 在 Golang 中的含义,nil 是 Go 编程语言中的零值,是众所周知且重要的预定义标识符。
Golang 中的 Lambda 表达式
Publish Date:2023/04/27 Views:699 Category:Go
-
本篇文章介绍如何在 Golang 中创建 lambda 表达式。Lambda 表达式似乎不存在于 Golang 中。 函数文字、lambda 函数或闭包是匿名函数的另一个名称。