dqde43215 2019-07-22 23:30
浏览 127
已采纳

如何以特定模式生成UUID

I am trying to generate a UUID in Go in a specific pattern. The program that I am working on requires various uuids to be generated similar to an existing uuid string. So the program would read an existing uuid (whose format can change in the future) and generate a new uuid in the same format to replace the existing one.

I have used the "github.com/satori/go.uuid" package to generate an uuid. The sample code that I am using is below which I found online.

As multiple instances of this programs would be deployed in parallel, I would like to avoid collisions or duplication in the uuids generated.

    package main

import (
"fmt"
"github.com/satori/go.uuid"
)

func main() {
    // Creating UUID Version 4
    // panic on error
    u1 := uuid.Must(uuid.NewV4(), nil)
    fmt.Printf("UUIDv4: %s
", u1)

    // or error handling
    u2:= uuid.NewV4()

    fmt.Printf("UUIDv4: %s
", u2)

    /* Formats needed: 
    2286664c688130096c9ce9008e4d97fb
    6abb173a-b134-49f2-aa88-9dff5dab12a1 (This is obtained from the above code)
    C8-3C-9C-64-61-70-62-B9-34-AC-9A-20-C9-EF-1D-6D
    */
}
  • 写回答

1条回答 默认 最新

  • duanba8070 2019-07-23 13:41
    关注

    UUIDs are 128 bits; that's a space large enough that collisions are staggeringly unlikely.

    From https://en.wikipedia.org/wiki/Universally_unique_identifier:

    When generated according to the standard methods, UUIDs are for practical purposes unique, without depending for their uniqueness on a central registration authority or coordination between the parties generating them, unlike most other numbering schemes. While the probability that a UUID will be duplicated is not zero, it is close enough to zero to be negligible.

    Thus, anyone can create a UUID and use it to identify something with near certainty that the identifier does not duplicate one that has already been, or will be, created to identify something else. Information labeled with UUIDs by independent parties can therefore be later combined into a single database or transmitted on the same channel, with a negligible probability of duplication.

    The whole point of UUIDs, the reason they are so large, is to avoid collisions without synchronization. Per the same article, in order to reach a 50% chance of a collision would require:

    generating 1 billion UUIDs per second for about 85 years, and a file containing this many UUIDs, at 16 bytes per UUID, would be about 45 exabytes, many times larger than the largest databases currently in existence, which are on the order of hundreds of petabytes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格