推薦包
https://github.com/satori/go.uuid
安裝
<code>go get github.com/satori/go.uuid/<code>
示例代碼
<code>package main
import (
\t"fmt"
\t"github.com/satori/go.uuid"
)
func main() {
\t// 創建UUID版本4
\t// 對錯誤報錯
\tu1 := uuid.Must(uuid.NewV4())
\tfmt.Printf("UUIDv4: %s\\n", u1)
\t// 或者錯誤處理
\tu2, err := uuid.NewV4()
\tif err != nil {
\t\tfmt.Printf("Something went wrong: %s", err)
\t\treturn
\t}
\tfmt.Printf("UUIDv4: %s\\n", u2)
\t// 從字符串輸入解析UUID
\tu2, err := uuid.FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
\tif err != nil {
\t\tfmt.Printf("Something went wrong: %s", err)
\t\treturn
\t}
\tfmt.Printf("Successfully parsed: %s", u2)
}/<code>
生成唯一uuid並轉為字符串
<code>uuid4 := uuid.Must(uuid.NewV4(), nil).String()/<code>
閱讀更多 項目分享者 的文章