DEV Community

Weerasak Chongnguluam
Weerasak Chongnguluam

Posted on

5 1

Go |> Generate และ Parse UUID ด้วย package "github.com/google/uuid"

UUID ย่อมาจาก Universally Unique IDentifier เป็นลำดับของตัวเลขและตัวอักษร ที่ตามอัลกอริทึมมันเนี่ย โอกาสที่จะซ้ำกันยากมาก ใน WIKI บอกเอาไว้ว่า

Thus, the probability to find a duplicate within 103 trillion version-4 UUIDs is one in a billion.

ซึ่งคือถวามน่าจะเป็นน้อยมากๆ

ทีนี้ถ้าใครอยากใช้ UUID ใน Go ก็มี package นึงจาก google เองคือ package "github.com/google/uuid" ที่ช่วยเรา generate UUID แล้วก็ช่วย Parse จาก string ให้กลายเป็น uuid.UUID ให้ด้วย นอกจากนั้นตัว type uuid.UUID ก็ยัง implement Mashal/Unmarsal เอาไว้ทำให้ใช้กับ encoding/json package แล้วมันจัดการแปลงเป็น string ให้เองแล้วด้วยตอนทำ json.Mashal หรือแปลงกลับมาเป็น uuid.UUID เองตอนทำ json.Unmarshal

Generate new UUID

วิธีสร้าง UUID ค่าใหม่ก็ใช้ง่ายๆคือเรียก uuid.New() เช่น

package main

import (
    "fmt"

    "github.com/google/uuid"
)

func main() {
    for i := 0; i < 10; i++ {
        id := uuid.New()
        fmt.Println(id)
    }
}
Enter fullscreen mode Exit fullscreen mode
$ go run main.go
f1cf86f5-c5ad-47ba-baf1-9ca646b91148
8cd3fc0b-7299-4a7c-a90a-1db749e01c20
e5c26dcb-8c6d-43c1-bc24-f4e195d78299
910dfb0d-202e-4a17-b6f0-c4e03c45bede
162da813-92f5-41fb-b1fd-4e2d1a809345
ff7f7b7a-167f-4f0d-8192-06c9df67b6dc
b752f0c3-908b-49fb-b6c6-01d33c0eb2a3
da4dfe12-8661-41b9-8a1c-ffebab5ff537
b8d9b607-49c1-4900-9674-a7746f270a39
ddd85702-90e1-419c-8afd-b79c1fde37fd
Enter fullscreen mode Exit fullscreen mode

Parse string to uuid.UUID

ทีนี้ถ้าเรามี string ที่อยู่ใน format UUID เช่น ddd85702-90e1-419c-8afd-b79c1fde37fd ที่อาจจะมาใน request path param เราสามารถแปลงเป็น uuid.UUID type ได้โดยใช้ฟังก์ชัน uuid.Parse แบบนี้

package main

import (
    "fmt"
    "log"

    "github.com/google/uuid"
)

func main() {
    strID := "ddd85702-90e1-419c-8afd-b79c1fde37fd"
    id, err := uuid.Parse(strID)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%T\n", id)
    fmt.Println(id)
}
Enter fullscreen mode Exit fullscreen mode
$ go run main.go
uuid.UUID
ddd85702-90e1-419c-8afd-b79c1fde37fd
Enter fullscreen mode Exit fullscreen mode

ยังมีฟังก์ชันอื่นๆในการใช้งาน package "github.com/google/uuid" อีก สามารถดูกันต่อได้ที่ document ของ package ที่ลิ้งนี้ https://pkg.go.dev/github.com/google/uuid

ขอฝาก Buy Me a Coffee

สำหรับท่านใดที่อ่านแล้วชอบโพสต์ต่างๆของผมที่นี่ ต้องการสนับสนุนค่ากาแฟเล็กๆน้อยๆ สามารถสนับสนุนผมได้ผ่านทาง Buy Me a Coffee คลิ๊กที่รูปด้านล่างนี้ได้เลยครับ

Buy Me A Coffee

ส่วนท่านใดไม่สะดวกใช้บัตรเครดิต หรือ Paypal สามารถสนับสนุนผมได้ผ่านทาง PromptPay โดยดู QR Code ได้จากโพสต์ที่พินเอาไว้ได้ที่ Page DevDose ครับ https://web.facebook.com/devdoseth

ขอบคุณครับ 🙏

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay