当前位置:网站首页>Golang:[]byte to string
Golang:[]byte to string
2022-07-02 19:22:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
golang in , Character slices []byte convert to string The easiest way to do it is
package main
import (
"fmt"
_ "unsafe"
)
func main() {
bytes := []byte("I am byte array !")
str := string(bytes)
bytes[0] = 'i'// Pay attention to this line ,bytes The data is modified here , however str The printed ones are still unchanged ,
fmt.Println(str)
}
Print information :I am byte array !
The above code bytes The data is modified here , however str The printed ones are still unchanged , Because []byte Memory of has been copied . Now let's look at the following code
package main
import (
"fmt"
"unsafe"
)
func main() {
bytes := []byte("I am byte array !")
str := (*string)(unsafe.Pointer(&bytes))
bytes[0] = 'i'
fmt.Println(*str)
}
Print information :i am byte array !
Now the printed information has changed , Now you can see str and bytes Share a piece of memory .
The point of this is , In network communication , Most acceptance methods are []byte, If []byte The data is relatively large , Memory copy will affect the performance of the system .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/148601.html Link to the original text :https://javaforall.cn
边栏推荐
- GMapping代码解析[通俗易懂]
- Digital scroll strip animation
- Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比
- 2022.7.1-----leetcode.241
- ORA-01455: converting column overflows integer datatype
- 【ERP软件】ERP体系二次开发有哪些危险?
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- 性能测试如何创造业务价值
- 机器学习笔记 - 时间序列预测研究:法国香槟的月销量
- MySQL advanced (Advanced) SQL statement
猜你喜欢
数据降维——主成分分析
MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
Excel finds the same value in a column, deletes the row or replaces it with a blank value
Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
PHP parser badminton reservation applet development requires online system
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
机器学习笔记 - 时间序列预测研究:法国香槟的月销量
【JVM调优实战100例】02——虚拟机栈与本地方法栈调优五例
线程应用实例
注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
随机推荐
《病人家属,请来一下》读书笔记
ICDE 2023|TKDE Poster Session(CFP)
Processing strategy of message queue message loss and repeated message sending
二进制操作
横向越权与纵向越权[通俗易懂]
医院在线问诊源码 医院视频问诊源码 医院小程序源码
论文导读 | 机器学习在数据库基数估计中的应用
页面标题组件
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
Learning summary of MySQL advanced 6: concept and understanding of index, detailed explanation of b+ tree generation process, comparison between MyISAM and InnoDB
yolov3 训练自己的数据集之生成train.txt
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
Qpropertyanimation use and toast case list in QT
全志A33使用主线U-Boot
仿京东放大镜效果(pink老师版)
高频面试题
【JVM调优实战100例】01——JVM的介绍与程序计数器
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
Progress-进度条
开发固定资产管理系统,开发固定资产管理系统用什么语音