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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?