当前位置:网站首页>golang:[]byte转string
golang:[]byte转string
2022-07-02 18:09:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
golang中,字符切片[]byte转换成string最简单的方式是
package main
import (
"fmt"
_ "unsafe"
)
func main() {
bytes := []byte("I am byte array !")
str := string(bytes)
bytes[0] = 'i'//注意这一行,bytes在这里修改了数据,但是str打印出来的依然没变化,
fmt.Println(str)
}打印信息:I am byte array !
上面的代码bytes在这里修改了数据,但是str打印出来的依然没变化,是因为[]byte的内存被拷贝了。接着看下面的代码
package main
import (
"fmt"
"unsafe"
)
func main() {
bytes := []byte("I am byte array !")
str := (*string)(unsafe.Pointer(&bytes))
bytes[0] = 'i'
fmt.Println(*str)
}打印信息:i am byte array !
现在打印出来的信息已经是改变过的了,现在可以看出来str和bytes共用一片内存。
这样做的意义在于,在网络通信中,大多数的接受方式都是[]byte,如果[]byte的数据比较大,内存拷贝的话会影响系统的性能。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/148601.html原文链接:https://javaforall.cn
边栏推荐
- 日期工具类(不定时更新)
- C的内存管理
- 高级性能测试系列《24. 通过jdbc执行sql脚本》
- 消息队列消息丢失和消息重复发送的处理策略
- M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
- 电商系统中常见的 9 大坑,你踩过没?
- Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
- reduce--遍历元素计算 具体的计算公式需要传入 结合BigDecimal
- metric_logger小解
- R language dplyr package Na_ The if function converts the control in the vector value into the missing value Na, and converts the specified content into the missing value Na according to the mapping r
猜你喜欢

PyTorch函数中的__call__和forward函数

Talk about the design of red envelope activities in e-commerce system
![27: Chapter 3: develop Passport Service: 10: [registration / login] interface: after the registration / login is OK, save the user session information (uid, utoken) to redis and cookies; (one main poi](/img/b9/2066a13b160252114c2881007094f8.png)
27: Chapter 3: develop Passport Service: 10: [registration / login] interface: after the registration / login is OK, save the user session information (uid, utoken) to redis and cookies; (one main poi

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

论文导读 | 机器学习在数据库基数估计中的应用

M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)

云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统

STM32G0 USB DFU 升级校验出错-2

According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors

MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
随机推荐
Stm32g0 USB DFU upgrade verification error -2
2022 software engineering final exam recall Edition
R language uses lrtest function of epidisplay package to perform likelihood ratio test on multiple GLM models (logisti regression). Compare whether the performance of the two models is different, and
Kubernetes three open interfaces first sight
R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
Yolov3 trains its own data set to generate train txt
Markdown基础语法
R语言dplyr包na_if函数把向量数值中的控制转化为缺失值NA、按照映射规则把指定内容转化为缺失值NA
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
ORA-01455: converting column overflows integer datatype
Date tool class (updated from time to time)
二进制操作
高级性能测试系列《24. 通过jdbc执行sql脚本》
Golang并发编程——goroutine、channel、sync
Imitation Jingdong magnifying glass effect (pink teacher version)
[fluent] dart data type (VaR data type | object data type)
When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
How performance testing creates business value