Home > Backend Development > Golang > How Can I Compare Function Pointers for Equality in Go?

How Can I Compare Function Pointers for Equality in Go?

Patricia Arquette
Release: 2024-12-28 13:40:12
Original
945 people have browsed it

How Can I Compare Function Pointers for Equality in Go?

Function Pointer Equality in Go

In Go, comparing two non-nil function pointers for equality using the standard pointer equality operator == has become invalid in recent versions. This is a departure from the pre-Go1 behavior where function pointers could be compared for identity.

Rationale for Not Allowing Pointer Equality of Functions

The prohibition on comparing function pointers for equality was motivated by the following reasons:

  • Performance: Functions may be implemented as closures, which do not capture variables from their environment. Allowing identity comparisons would require the runtime to create a new closure for each function, resulting in performance overhead.
  • Clarity: To avoid mixing equality and identity, the Go1 operators == and != only compare values for equivalence, not identity. This ensures a consistent approach throughout the language.

Achieving Pointer Equality of Functions

While direct pointer comparison of functions is no longer allowed, there are alternative approaches to achieve the desired behavior:

  • Assign Unique Variables: Declare separate variables for each function and compare their addresses:
package main

import "fmt"

func F1() {}
func F2() {}

var F1_ID = F1  // Create a *unique* variable for F1
var F2_ID = F2  // Create a *unique* variable for F2

func main() {
    f1 := &F1_ID  // Take the address of F1_ID
    f2 := &F2_ID  // Take the address of F2_ID

    fmt.Println(f1 == f1)  // Prints true
    fmt.Println(f1 == f2)  // Prints false
}
Copy after login
  • Use Reflection: The reflect package provides low-level access to object information, including function pointers. You can use it to compare function identities:
package main

import "fmt"
import "reflect"

func SomeFun() {}
func AnotherFun() {}

func main() {
    sf1 := reflect.ValueOf(SomeFun)
    sf2 := reflect.ValueOf(SomeFun)
    fmt.Println(sf1.Pointer() == sf2.Pointer())  // Prints true

    af1 := reflect.ValueOf(AnotherFun)
    fmt.Println(sf1.Pointer() == af1.Pointer())  // Prints false
}
Copy after login

Note: Using reflect relies on undefined behavior. It does not guarantee consistency across platforms or Go versions.

The above is the detailed content of How Can I Compare Function Pointers for Equality in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template