Go实战--golang生成uuid The way to go
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.****.net/jiangjunshow
生命不止,继续 go go go !!!
在做server开发的时候,难免用到uuid,这里与大家分享一下golang中如何生成uuid。
什么是uuid?
uuid是Universally Unique Identifier的缩写,即通用唯一识别码。
uuid的目的是让分布式系统中的所有元素,都能有唯一的辨识资讯,而不需要透过*控制端来做辨识资讯的指定。如此一来,每个人都可以建立不与其它人冲突的 uuid。
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems.
例如java中生成uuid:
package com.mytest;import java.util.UUID;public class UTest { public static void main(String[] args) { UUID uuid = UUID.randomUUID(); System.out.println(uuid);}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
c++中生成uuid:
#pragma comment(lib, "rpcrt4.lib")#include <windows.h>#include <iostream>using namespace std;int main(){ UUID uuid; UuidCreate(&uuid); char *str; UuidToStringA(&uuid, (RPC_CSTR*)&str); cout<<str<<endl; RpcStringFreeA((RPC_CSTR*)&str); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
github.com/satori/go.uuid
目前,golang中的uuid还没有纳入标准库,我们使用github上的开源库:
go get -u github.com/satori/go.uuid
- 1
使用:
package mainimport ( "fmt" "github.com/satori/go.uuid")func main() { // 创建 u1 := uuid.NewV4() fmt.Printf("UUIDv4: %s\n", u1) // 解析 u2, err := uuid.FromString("f5394eef-e576-4709-9e4b-a7c231bd34a4") if err != nil { fmt.Printf("Something gone wrong: %s", err) return } fmt.Printf("Successfully parsed: %s", u2)}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
uuid在websocket中使用
这里就是一个简单的使用而已,在websocket中为每一个连接的客户端分配一个uuid。
golang中可以使用github.com/gorilla/websocket为我们提供的websocket开发包。
声明一个客户端结构体:
type Client struct { id string socket *websocket.Conn send chan []byte}
- 1
- 2
- 3
- 4
- 5
使用:
client := &Client{id: uuid.NewV4().String(), socket: conn, send: make(chan []byte)}
- 1
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.****.net/jiangjunshow
生命不止,继续 go go go !!!
在做server开发的时候,难免用到uuid,这里与大家分享一下golang中如何生成uuid。
什么是uuid?
uuid是Universally Unique Identifier的缩写,即通用唯一识别码。
uuid的目的是让分布式系统中的所有元素,都能有唯一的辨识资讯,而不需要透过*控制端来做辨识资讯的指定。如此一来,每个人都可以建立不与其它人冲突的 uuid。
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems.
例如java中生成uuid:
package com.mytest;import java.util.UUID;public class UTest { public static void main(String[] args) { UUID uuid = UUID.randomUUID(); System.out.println(uuid);}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
c++中生成uuid:
#pragma comment(lib, "rpcrt4.lib")#include <windows.h>#include <iostream>using namespace std;int main(){ UUID uuid; UuidCreate(&uuid); char *str; UuidToStringA(&uuid, (RPC_CSTR*)&str); cout<<str<<endl; RpcStringFreeA((RPC_CSTR*)&str); return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
github.com/satori/go.uuid
目前,golang中的uuid还没有纳入标准库,我们使用github上的开源库:
go get -u github.com/satori/go.uuid
- 1
使用:
package mainimport ( "fmt" "github.com/satori/go.uuid")func main() { // 创建 u1 := uuid.NewV4() fmt.Printf("UUIDv4: %s\n", u1) // 解析 u2, err := uuid.FromString("f5394eef-e576-4709-9e4b-a7c231bd34a4") if err != nil { fmt.Printf("Something gone wrong: %s", err) return } fmt.Printf("Successfully parsed: %s", u2)}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
uuid在websocket中使用
这里就是一个简单的使用而已,在websocket中为每一个连接的客户端分配一个uuid。
golang中可以使用github.com/gorilla/websocket为我们提供的websocket开发包。
声明一个客户端结构体:
type Client struct { id string socket *websocket.Conn send chan []byte}
- 1
- 2
- 3
- 4
- 5
使用:
client := &Client{id: uuid.NewV4().String(), socket: conn, send: make(chan []byte)}
- 1