go 源码分析string、[]byte的相互转换

string

简单的来说字符串是一系列8位字节的集合,通常但不一定代表UTF-8编码的文本。字符串可以为空,但不能为nil。而且字符串的值是不能改变的。

  • 不同的语言字符串有不同的实现,在go的源码中src/runtime/string.gostring的定义如下:
// string is the set of all strings of 8-bit bytes, conventionally but not
// necessarily representing UTF-8-encoded text. A string may be empty, but
// not nil. Values of string type are immutable.

type stringStruct struct {
	str unsafe.Pointer
	len int
}

可以看到str其实是个指针,指向某个数组的首地址,另一个字段是len长度。其实指向的就是byte数组。

  • string的实例化:注意string其实就是个struct。在实例化这个stringStruct的时候,源码如下
func gostringnocopy(str *byte) string {
	ss := stringStruct{str: unsafe.Pointer(str), len: findnull(str)}
	s := *(*string)(unsafe.Pointer(&ss))
	return s
}
  • string 底层是一个包含多个字节(1字节=8bit)的集合。
  • string 类型的值是不可改变的(这里的值不变指的是变量的数据地址,即 str 的值不变)
  • string结构体的str指针指向的是一个字符常量的地址, 这个地址里面的内容是不可以被改变的,因为它是只读的,但是这个指针可以指向不同的地址。

关联数据结构

  • string 可以被拆分为一个包含多个字节的序列,如:

    str := "ben生而平凡"
    fmt.Println([]byte(str))
    
    [98 101 110 231 148 159 232 128 140 229 185 179 229 135 161]
    
  • string 可以被拆分为一个包含多个字符的序列,如:

    str := "ben生而平凡"
    fmt.Println([]rune(str))
    
    [98 101 110 29983 32780 24179 20961]
    
  • 我们通常说的字符是指 Unicode 字符。‘G’, ‘o’, ‘菜’, ‘鸟’ 都是一个字符。一个字符可以是只包含一个字节(像:‘G’, ‘o’),也可以是包含多个字节(像:‘菜’, ‘鸟’)。

  • byte:[]byte和string的差别是更改变量的时候array的内容可以被更改。

    // byte is an alias for uint8 and is equivalent to uint8 in all ways. It is
    // used, by convention, to distinguish byte values from 8-bit unsigned
    // integer values.
    type byte = uint8
    
  • rune

    // rune is an alias for int32 and is equivalent to int32 in all ways. It is
    // used, by convention, to distinguish character values from integer values
    type rune = int32
    
  • slice : slice结构在go的源码中src/runtime/slice.go定义:

    type slice struct {
    	array unsafe.Pointer
    	len   int
    	cap   int
    }
    

    array是数组的指针,len表示长度,cap表示容量。除了cap,其他看起来和string的结构很像。

相互转换

  • 将string转为[]byte,语法[]byte(string)源码如下:可以看到b是新分配的,然后再将s复制给b

    func stringtoslicebyte(buf *tmpBuf, s string) []byte {
    	var b []byte
    	if buf != nil && len(s) <= len(buf) {
    		*buf = tmpBuf{}
    		b = buf[:len(s)]
    	} else {
    		b = rawbyteslice(len(s))
    	}
    	copy(b, s)
    	return b
    }
    
  • 将[]byte转为string,语法string([]byte)源码如下:

    func slicebytetostring(buf *tmpBuf, b []byte) string {
    	l := len(b)
    	if l == 0 {
    		// Turns out to be a relatively common case.
    		// Consider that you want to parse out data between parens in "foo()bar",
    		// you find the indices and convert the subslice to string.
    		return ""
    	}
    	if raceenabled && l > 0 {
    		racereadrangepc(unsafe.Pointer(&b[0]),
    			uintptr(l),
    			getcallerpc(unsafe.Pointer(&buf)),
    			funcPC(slicebytetostring))
    	}
    	if msanenabled && l > 0 {
    		msanread(unsafe.Pointer(&b[0]), uintptr(l))
    	}
    	s, c := rawstringtmp(buf, l)
    	copy(c, b)
    	return s
    }
    
    func rawstringtmp(buf *tmpBuf, l int) (s string, b []byte) {
    	if buf != nil && l <= len(buf) {
    		b = buf[:l]
    		s = slicebytetostringtmp(b)
    	} else {
    		s, b = rawstring(l)
    	}
    	return
    }
    
    func rawstring(size int) (s string, b []byte) {
    	p := mallocgc(uintptr(size), nil, false)
    
    	stringStructOf(&s).str = p
    	stringStructOf(&s).len = size
    
    	*(*slice)(unsafe.Pointer(&b)) = slice{p, size, size}
    
    	return
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Go中,可以使用标准转换或强转换byte转换string。标准转换涉及底层数组的拷贝,而强转换则直接替换指针的指向,使得string和[]byte指向同一个底层数组。因此,强转换的性能更好。 以下是使用标准转换和强转换byte转换string的示例代码: 标准转换: ``` b := []byte{104, 101, 108, 108, 111} // byte数组 s := string(b) // 转换string ``` 强转换: ``` b := []byte{104, 101, 108, 108, 111} // byte数组 s := *(*string)(unsafe.Pointer(&b)) // 强转换string ``` 请注意,强转换需要使用`unsafe`包,因此需要谨慎使用,并确保操作的安全性和正确性。 参考资料: 对于标准转换,无论是从[]bytestring还是string转[]byte都会涉及底层数组的拷贝。而强转换是直接替换指针的指向,从而使得string和[]byte指向同一个底层数组。这样,当然后者的性能会更好。 golang []bytestring相互转换_墨痕诉清风的博客-CSDN博客_golang字符串转byte数组<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Golang中[]bytestring转换全解析](https://blog.csdn.net/slphahaha/article/details/109405685)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [10-【go】golang []bytestring相互转换](https://blog.csdn.net/qq_42303254/article/details/116981159)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值