Go:再次将类型 uuid.UUID (satori) 的 reflect.Value 转换回 uuid.UUID
Go: convert reflect.Value of type uuid.UUID (satori) back to uuid.UUID again
我试图再次将反射的 UUID 转换回实际的 UUID 对象,但找不到方法,当我打印反射值时它看起来是正确的,但在尝试转换时我找不到方式。
package main
import (
"fmt"
"reflect"
uuid "github.com/satori/go.uuid"
)
func main() {
value := uuid.Must(uuid.NewV4())
reflectedValue := reflect.ValueOf(value)
fmt.Println(reflectedValue)
result := reflectedValue.String()
fmt.Println(result)
}
输出:
$ go run main.go
0cca93f8-1763-4816-a2a0-09e7aeeb674c
<uuid.UUID Value>
如何将 reflectedValue 直接转换为 uuid.UUID 或 []byte/string 以便我可以从中创建一个 uuid.UUID 对象。由于 fmt.Println 设法打印出正确的值,因此必须能够进行转换,但我不知道如何进行转换。
uuid.UUID 对象似乎具有 16 个 uint8 值的数据结构。
使用type assertion on the underlying value:
u, ok := reflectedValue.Interface().(uuid.UUID)
我试图再次将反射的 UUID 转换回实际的 UUID 对象,但找不到方法,当我打印反射值时它看起来是正确的,但在尝试转换时我找不到方式。
package main
import (
"fmt"
"reflect"
uuid "github.com/satori/go.uuid"
)
func main() {
value := uuid.Must(uuid.NewV4())
reflectedValue := reflect.ValueOf(value)
fmt.Println(reflectedValue)
result := reflectedValue.String()
fmt.Println(result)
}
输出:
$ go run main.go
0cca93f8-1763-4816-a2a0-09e7aeeb674c
<uuid.UUID Value>
如何将 reflectedValue 直接转换为 uuid.UUID 或 []byte/string 以便我可以从中创建一个 uuid.UUID 对象。由于 fmt.Println 设法打印出正确的值,因此必须能够进行转换,但我不知道如何进行转换。
uuid.UUID 对象似乎具有 16 个 uint8 值的数据结构。
使用type assertion on the underlying value:
u, ok := reflectedValue.Interface().(uuid.UUID)