| 12345678910111213141516171819202122232425262728293031323334353637 |
- package game
- import (
- "fmt"
- "runtime"
- "strconv"
- "strings"
- "sync"
- )
- //获取线程ID
- func GoID() int {
- var buf [64]byte
- n := runtime.Stack(buf[:], false)
- idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0]
- id, err := strconv.Atoi(idField)
- if err != nil {
- panic(fmt.Sprintf("cannot get goroutine id: %v", err))
- }
- return id
- }
- var _uuidIntMap = make(map[string]int)
- var _uuidIntLock sync.Mutex
- //获取int型的自增ID
- func GetUuidInt(key string) int {
- _uuidIntLock.Lock()
- id := _uuidIntMap[key]
- nextID := id + 1
- if nextID > 1000000000 {
- nextID = 0
- }
- _uuidIntMap[key] = nextID
- _uuidIntLock.Unlock()
- return id
- }
|