Go language: basic usage of Google's uuid module, google

uuid is to generate the 16-byte UUID modules developed by Google, implements RFC4122 ; V1 of a UUID, v2, v3, v4, v5, etc. Each version achieved. Generally, if only to generate a unique serial number, then usually v4 version, New () and NewRandom () can generate v4, New encapsulation of NewRandom is put into panics error processing, but does not almost theoretically It may occur panics's.

func New() UUID
func NewRandom() (UUID, error)

Examples are as follows:

package main

import (
	"fmt"
	"github.com/google/uuid"
)

func main() {
	for i := 0; i < 10; i++ {
		id := uuid.New()
		fmt.Printf("%s %s\n", id, id.Version().String())
	}

	for i := 0; i < 10; i++ {
		id2,err := uuid.NewRandom()
		if err != nil{
			fmt.Printf("%v\n", err)
		}
		fmt.Printf("%s %s\n", id2, id2.Version().String())
	}
}

Output:

adba0f28-8489-4d99-a5c8-a12eb6c960c9 VERSION_4
0b92bb34-34b2-4f21-9bf8-70ac181b39b5 VERSION_4
177b9491-2ad2-4396-8fd3-f7440404f46f VERSION_4
c098c60b-4f98-4fe3-9fc3-7cec9a7c0717 VERSION_4
0b58b54c-a941-47c9-a889-5871e65b223d VERSION_4
96203b6d-b084-483b-af92-e5b2443cc29f VERSION_4
e5b32605-e017-4a22-9acb-5841ad28ebdd VERSION_4
15f9680e-432e-4faa-b631-53b6f7e70e9a VERSION_4
40f82f91-ef81-4303-a59c-58651f972795 VERSION_4
221e71ec-e9d1-4632-a3f6-0275b3a288ae VERSION_4
c1d4908a-9b9c-44fd-90f0-7e232f0e5ff1 VERSION_4
6027e676-b46a-4281-a1e0-b3bf6291998a VERSION_4
da589a0e-e172-4dac-bc4c-fc50c9624a6e VERSION_4
9c1fe414-3bec-4b7a-9187-b80ee97a1e67 VERSION_4
93fc7459-31c2-4dcb-be9a-e3c80d82453d VERSION_4
5339d058-7241-4f74-a6e3-27ad5d32ba74 VERSION_4
ed4efa2b-2954-4f46-bb0c-b65250494bde VERSION_4
a98c5172-a302-4ef2-967e-1029604ffd1f VERSION_4
b316af48-0c67-48bc-bba1-59c8d18f7685 VERSION_4
e4e39e07-3e15-4117-b572-73d553548249 VERSION_4

Said New method may be panics, but the source code to see essentially no error possible on official documents. Will depend error "crypto / rand" The Reader can read at least to 16 random bytes, while Reader is an operating system from Linux getRandom (2) to read if not from  / dev / urandom read, getrandom (2) of the said documents within 256 bytes will not be interrupted by signals, and / dev / urandom document did not say there may be an error of. So in theory there is no error possible.

 

Uuid to use other versions, see:

"Go languages: Google google's uuid module for various versions of realization uuid (version 1, version 2, version 3, verison 4, version 5)"

Published 51 original articles · won praise 3 · views 40000 +

Guess you like

Origin blog.csdn.net/pengpengzhou/article/details/105269061