Implements reflect.Indirect function to support pointer dereferencing. This function returns the value that a pointer points to, or returns the value unchanged if it's not a pointer. Fixes #1354 Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <luoliwoshang@users.noreply.github.com>
25 lines
504 B
Go
25 lines
504 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
)
|
|
|
|
func main() {
|
|
x := 42
|
|
p := &x
|
|
|
|
// Test 1: Non-pointer value - should return same value
|
|
v1 := reflect.Indirect(reflect.ValueOf(x))
|
|
fmt.Printf("Non-pointer: %v\n", v1.Interface())
|
|
|
|
// Test 2: Pointer - should dereference
|
|
v2 := reflect.Indirect(reflect.ValueOf(p))
|
|
fmt.Printf("Pointer: %v\n", v2.Interface())
|
|
|
|
// Test 3: Nil pointer
|
|
var nilPtr *int
|
|
v3 := reflect.Indirect(reflect.ValueOf(nilPtr))
|
|
fmt.Printf("Nil pointer valid: %v\n", v3.IsValid())
|
|
}
|