No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

id.go 690B

12345678910111213141516171819202122232425262728293031323334353637
  1. package botserv
  2. import (
  3. "crypto/md5"
  4. "fmt"
  5. "io"
  6. "time"
  7. )
  8. // This thing contains a channel that when initialized (see NewIdGenerator)
  9. // will return a bunch of (as best as I can tell) unique md5 hashes.
  10. //
  11. // we use this for naming players, games, etc.
  12. //
  13. // It will consume a single goroutine
  14. type IdGenerator struct {
  15. id chan int64
  16. }
  17. func NewIdGenerator() *IdGenerator {
  18. g := IdGenerator{}
  19. g.id = make(chan int64)
  20. go func() {
  21. var i int64
  22. for i = 0; ; i++ {
  23. g.id <- i
  24. }
  25. }()
  26. return &g
  27. }
  28. func (id *IdGenerator) Hash() string {
  29. h := md5.New()
  30. ns := time.Now().UnixNano() + <-id.id
  31. io.WriteString(h, fmt.Sprintf("%d", ns))
  32. return fmt.Sprintf("%x", h.Sum(nil))[:8]
  33. }