added a hash generator
This commit is contained in:
parent
0512bb7664
commit
922acbdb54
31
id.go
Normal file
31
id.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
type IdGenerator struct {
|
||||
id chan int64
|
||||
}
|
||||
|
||||
func NewIdGenerator() *IdGenerator {
|
||||
g := IdGenerator{}
|
||||
g.id = make(chan int64)
|
||||
go func() {
|
||||
var i int64
|
||||
for i = 0; ; i++ {
|
||||
g.id <- i
|
||||
}
|
||||
}()
|
||||
return &g
|
||||
}
|
||||
|
||||
func (id *IdGenerator) Hash() string {
|
||||
h := md5.New()
|
||||
ns := time.Now().UnixNano() + <-id.id
|
||||
io.WriteString(h, fmt.Sprintf("%d", ns))
|
||||
return fmt.Sprintf("%x", h.Sum(nil))
|
||||
}
|
Loading…
Reference in New Issue
Block a user