Thumbnail image

避免内存拷贝的字符串与字节串互转(Go)

  • 正常强制转换:

    func s2b(s string) []byte {
        return []byte(s)
    }
    func b2s(b []byte) string {
        return string(b)
    }
    
  • 避免内存拷贝:

    func s2b(s string) (b []byte) {
        /* #nosec G103 */
        bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
        /* #nosec G103 */
        sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
        bh.Data = sh.Data
        bh.Cap = sh.Len
        bh.Len = sh.Len
        return b
    }
    
    func b2s(b []byte) string {
        /* #nosec G103 */
        return *(*string)(unsafe.Pointer(&b))
    }