doujia4619 2014-06-17 13:31 采纳率: 0%
浏览 129
已采纳

开始:有效地将十六进制值存储在内存中

I have two 16-byte hex values that relate to each other that I want to keep in memory in Go (so they only need to exist for the lifetime of the running process) that can be represented as a simple map like so:

{"aabbcc": "112233"}

Obviously I could represent these as a struct of two strings, but I'm just wondering if there's a faster (i.e. performance) or more memory-efficient way to store the strings in Go? I've only delved into Go lightly so far, so don't know the standard library well enough to know a good answer.

Edit: For an idea of what I'm getting at (in pseudo-code):

I've got two UUIDs from different sources, that are generated/received as strings:

uuid_a_1 = "aabb-1122-3344"
uuid_a_2 = "ddee-5566-7788"

I want to store these in relation to each other:

uuid_map[] = {uuid_a_1: uuid_a_2}

So that I can return one when I lookup the other:

return uuid_map[uuid_a_1]

I'm just curious if there's a more efficient way to store these in memory than a simple map of strings, as I may for example want to store several thousand during the lifetime of the process, and want to be able to key/value store these as quickly as possible (the idea being that because I know the exact size and type of the keys and values that I can do it fast).

  • 写回答

1条回答 默认 最新

  • doulin4844 2014-06-17 17:04
    关注

    As FUZxxl is suggesting you can encode the string to a byte array.

    http://play.golang.org/p/7MYWTWSu2-

    package main
    
    import (
        "encoding/hex"
        "fmt"
        "strings"
    )
    
    func main() {
        b, err := hex.DecodeString(strings.Replace("df31a780-f640-11e3-a3ac-0800200c9a66", "-", "", -1))
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println("Decoded:", b)  
        fmt.Println("Encoded:", hex.EncodeToString(b))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测