Home > Article > Backend Development > Pitfalls and best practices of Golang function pointers
Function pointer pitfalls and best practices in Go: Pitfall: Pointer pointing to unavailable function Best practice: Use local variables or closures to capture function values. Pitfall: Modify the function pointed to by the pointer Best practice: Keep the function pointer unmodifiable and create a new function in another closure. Practical case: callback function For example, use a function pointer to create a log function that takes the log message and severity level as parameters of the callback function.
Traps and Best Practices of Function Pointers in Go
In Go, a function pointer is a function that takes a function as a value A powerful mechanism for delivery. However, there are some traps to be aware of when using function pointers:
Trap 1: Function pointer points to an unavailable function
When a function pointer points to a function that is no longer available, A dangling pointer error will occur:
func newFunc() { // ... } func main() { newFuncPtr := newFunc // 将函数 newFunc 赋值给指针 newFuncPtr newFunc = nil // 将 newFunc 设为 nil,使其不可用 newFuncPtr() // 这会触发悬空指针错误 }
Best Practice: Capture function values using local variables or closures
To avoid dangling pointers, capture function values locally In variables or closures:
func main() { newFunc := newFunc newFunc() // 仍然有效,因为它捕获了 newFunc 的副本 }
Trap 2: Modify the function pointed to by the pointer
Modifying the function pointed to by the function pointer may lead to unexpected consequences:
type API interface { Call() } func makeAPI() API { return func() { fmt.Println("Hello") } } func main() { apiPtr := makeAPI() apiPtr = func() { fmt.Println("Goodbye") } apiPtr.Call() // 输出 "Goodbye" }
Best practice: Keep function pointers unmodifiable
If you need to modify the function behavior, create a new function in another closure:
func main() { api := makeAPI() api = func(a API) API { return func() { fmt.Println("Goodbye") a.Call() } }(api) api.Call() // 输出 "Hello", 然后是 "Goodbye" }
Practical case: callback function
The callback function is a common example of a function pointer, which is used to notify the caller when an event occurs.
For example, we can use a function pointer to build a logging function that takes the log message and severity level as arguments to the callback function:
import "fmt" import "log" type Severity string const ( Info Severity = "INFO" Warning Severity = "WARNING" Error Severity = "ERROR" ) func Log(severity Severity, message string) { log.Println(fmt.Sprintf("%s: %s", severity, message)) } // 获取 severity 为 Info 的日志函数 func InfoLogger() func(string) { return func(message string) { Log(Info, message) } }
We can then use the callback function to pass the log message Sent to log function:
func main() { infoLogger := InfoLogger() infoLogger("Hello, world!") // 输出:INFO: Hello, world! }
The above is the detailed content of Pitfalls and best practices of Golang function pointers. For more information, please follow other related articles on the PHP Chinese website!