【Gin-v1.9.0源码阅读】internal/bytesconv/byteconv.go

// Copyright 2020 Gin Core Team. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package bytesconv

import (
	"unsafe"
)

// StringToBytes converts string to byte slice without a memory allocation.
// StringToBytes在没有内存分配的情况下将字符串转换为字节片。
func StringToBytes(s string) []byte {
	return *(*[]byte)(unsafe.Pointer(
		&struct {
			string
			Cap int
		}{s, len(s)},
	))
}

// BytesToString converts byte slice to string without a memory allocation.
// BytesToString在没有内存分配的情况下将字节片转换为字符串。
func BytesToString(b []byte) string {
	return *(*string)(unsafe.Pointer(&b))
}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/131029403