Skip to content

Instantly share code, notes, and snippets.

@veggiemonk
Created July 3, 2022 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veggiemonk/fc6e74de123905c5a06a3e76deb92332 to your computer and use it in GitHub Desktop.
Save veggiemonk/fc6e74de123905c5a06a3e76deb92332 to your computer and use it in GitHub Desktop.
[experimental] Encode UUID as string of numbers
package main
import (
"fmt"
"math/big"
"strings"
"github.com/google/uuid"
)
// Go Playground: https://go.dev/play/p/RBzt677Nqeq
func main() {
u1 := strings.ReplaceAll(uuid.New().String(), "-", "")
fmt.Println("UUID string:", u1)
// encode UUID as string of numbers
id := new(big.Int)
if _, ok := id.SetString(strings.ReplaceAll(uuid.New().String(), "-", ""), 16); ok {
u2 := id.String()
fmt.Println("UUID as numbers:", u2)
} else {
fmt.Errorf("failed to convert uuid to bigint")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment