dongwo5110 2018-06-12 22:49
浏览 124
已采纳

转到:再次将反射类型uuid.UUID(satori)类型的值转换回uuid.UUID

I'm trying to convert a reflected UUID back to an actual UUID object again but can't find a way, when I print the reflected value it looks correct, but when trying to convert I can't find a way.

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)
}

output:

$ go run main.go
0cca93f8-1763-4816-a2a0-09e7aeeb674c
<uuid.UUID Value>

How to convert reflectedValue to uuid.UUID directly or []byte/string so I can make a uuid.UUID object from that. Since fmt.Println manages to print the correct value it has to be possible to make the conversion, but I can't figure out how.

It seem like the uuid.UUID object has a data structure of 16 uint8 values.

  • 写回答

1条回答 默认 最新

  • duangutang3940 2018-06-12 22:55
    关注

    Use a type assertion on the underlying value:

    u, ok := reflectedValue.Interface().(uuid.UUID)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!