当前位置:网站首页>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
边栏推荐
- #gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
- R language uses Cox of epidisplay package Display function obtains the summary statistical information of Cox regression model (risk rate HR, adjusted risk rate and its confidence interval, P value of
- 新手必看,点击两个按钮切换至不同的内容
- 教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
- ICDE 2023|TKDE Poster Session(CFP)
- [test development] takes you to know what software testing is
- End-to-End Object Detection with Transformers(DETR)论文阅读与理解
- mysql备份后缀是什么_mysql备份还原
- Virtual machine initialization script, virtual machine mutual secret key free
- [0701] [paper reading] allowing data imbalance issue with perforated input during influence
猜你喜欢

codeforces每日5题(均1700)-第四天

论文导读 | 关于将预训练语言模型作为知识库的分析与批评

云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
![[fluent] dart data type (VaR data type | object data type)](/img/1b/fe2529af5f6663fad1fb7861f14ab5.jpg)
[fluent] dart data type (VaR data type | object data type)

How can retail enterprises open the second growth curve under the full link digital transformation

使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星

注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机

Kubernetes three open interfaces first sight

Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5

新手必看,點擊兩個按鈕切換至不同的內容
随机推荐
2022 compilation principle final examination recall Edition
C文件输入操作
Data dimensionality reduction principal component analysis
reduce--遍历元素计算 具体的计算公式需要传入 结合BigDecimal
Fastdfs installation
Markdown基础语法
电脑使用哪个录制视频软件比较好
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
Digital scroll strip animation
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
医院在线问诊源码 医院视频问诊源码 医院小程序源码
使用xml文件打印mybaties-log插件的方式
[fluent] dart data type (VaR data type | object data type)
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
#gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
Memory management of C
Emmet basic syntax
Preprocessing and preprocessing macros
聊聊电商系统中红包活动设计
Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal