当前位置:网站首页>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
边栏推荐
- The R language dplyr package rowwise function and mutate function calculate the maximum value of multiple data columns in each row in the dataframe data, and generate the data column (row maximum) cor
- Crypto usage in nodejs
- Mini Golf Course: a good place for leisure and tourism in London
- PyTorch函数中的__call__和forward函数
- [test development] takes you to know what software testing is
- 【JVM调优实战100例】03——JVM堆调优四例
- 页面标题组件
- Installation of thingsboard, an open source IOT platform
- 数据降维——主成分分析
- 【测试开发】一文带你了解什么是软件测试
猜你喜欢

Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5

新手必看,點擊兩個按鈕切換至不同的內容

Excel如何进行隔行复制粘贴

Novice must see, click two buttons to switch to different content

Stratégie touristique d'été de Singapour: un jour pour visiter l'île de San taosha à Singapour

Hospital online inquiry source code hospital video inquiry source code hospital applet source code

Compile oglpg-9th-edition source code with clion

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

STM32G0 USB DFU 升级校验出错-2

Yunna | why use the fixed asset management system and how to enable it
随机推荐
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
Use cheat engine to modify money, life and stars in Kingdom rush
Deep learning mathematics foundation
新手必看,點擊兩個按鈕切換至不同的內容
How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
Tips for material UV masking
Processing strategy of message queue message loss and repeated message sending
2022 compilation principle final examination recall Edition
Progress progress bar
Imitation Jingdong magnifying glass effect (pink teacher version)
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
[test development] software testing - concept
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
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
Excel查找一列中的相同值,删除该行或替换为空值
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
Use MNIST in tensorflow 2_ 784 data set for handwritten digit recognition
Mini Golf Course: a good place for leisure and tourism in London