12 Star 34 Fork 10

dreamans / guuid

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
machine.go 883 Bytes
一键复制 编辑 原始数据 按行查看 历史
dreamans 提交于 2018-03-21 16:42 . first commit
// This file is part of the guuid package
//
// (c) Dreamans <dreamans@163.com>
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
package guuid
import (
"os"
"strings"
"strconv"
"crypto/md5"
)
var machineId []byte = machineHash()
func machineHash() (machHash []byte) {
posixPID := posixPid()
machineName := machineHostname()
randId := rand()
machine := strings.Join([]string{
machineName,
strconv.Itoa(posixPID),
uint32ToHexString(randId),
}, ",")
md5Ctx := md5.New()
md5Ctx.Write([]byte(machine))
machHash = md5Ctx.Sum(nil)
return
}
func machineHostname() string {
host, err := os.Hostname()
if err != nil {
host = "localhost"
}
return host
}
func posixPid() int {
return int(os.Getpid())
}
Go
1
https://gitee.com/dreamans/guuid.git
git@gitee.com:dreamans/guuid.git
dreamans
guuid
guuid
7d0b13a43adc

搜索帮助