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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VMware安装虚拟网络驱动程序不成功,如何解决?(操作系统-linux)
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件