github.com/iikira/iikira-go-utils@v0.0.0-20230610031953-f2cb11cde33a/utils/bdcrypto/reverse.go (about) 1 package bdcrypto 2 3 import ( 4 "unsafe" 5 ) 6 7 // BytesReverse 反转字节数组, 此操作会修改原值 8 func BytesReverse(b []byte) []byte { 9 length := len(b) 10 for i := 0; i < length/2; i++ { 11 b[i], b[length-i-1] = b[length-i-1], b[i] 12 } 13 return b 14 } 15 16 // StringReverse 反转字符串, 此操作不会修改原值 17 func StringReverse(s string) string { 18 newBytes := make([]byte, len(s)) 19 copy(newBytes, s) 20 b := BytesReverse(newBytes) 21 return *(*string)(unsafe.Pointer(&b)) 22 }