golang-im聊天
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.

2 vuotta sitten
123456789101112131415161718192021222324252627282930313233
  1. package util
  2. import (
  3. "math/rand"
  4. "time"
  5. "unsafe"
  6. )
  7. var r *rand.Rand
  8. func init() {
  9. r = rand.New(rand.NewSource(time.Now().Unix()))
  10. }
  11. func Str2bytes(s string) []byte {
  12. x := (*[2]uintptr)(unsafe.Pointer(&s))
  13. h := [3]uintptr{x[0], x[1], x[1]}
  14. return *(*[]byte)(unsafe.Pointer(&h))
  15. }
  16. func Bytes2str(b []byte) string {
  17. return *(*string)(unsafe.Pointer(&b))
  18. }
  19. // RandString 生成随机字符串
  20. func RandString(len int) string {
  21. bytes := make([]byte, len)
  22. for i := 0; i < len; i++ {
  23. b := r.Intn(26) + 65
  24. bytes[i] = byte(b)
  25. }
  26. return string(bytes)
  27. }