当前位置:网站首页>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 difference between SLC, MLC, TLC and QLC NAND SSD: which is better?
- 全志A33使用主线U-Boot
- PyTorch函数中的__call__和forward函数
- Yolov3 trains its own data set to generate train txt
- metric_logger小解
- 以太网PHY层芯片LAN8720A简介
- ORA-01455: converting column overflows integer datatype
- 中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
- 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
- Crypto usage in nodejs
猜你喜欢
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
Stm32g0 USB DFU upgrade verification error -2
Codeworks 5 questions per day (1700 average) - day 4
How to copy and paste interlaced in Excel
9D电影是怎样的?(+维度空间常识)
Excel如何进行隔行复制粘贴
随机推荐
拦截器与过滤器的区别
2022.7.1-----leetcode.241
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
Use cheat engine to modify money, life and stars in Kingdom rush
Stratégie touristique d'été de Singapour: un jour pour visiter l'île de San taosha à Singapour
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
R language ggplot2 visualization: gganimate package creates dynamic histogram animation (GIF) and uses transition_ The States function displays a histogram step by step along a given dimension in the
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
Imitation Jingdong magnifying glass effect (pink teacher version)
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
R语言dplyr包na_if函数把向量数值中的控制转化为缺失值NA、按照映射规则把指定内容转化为缺失值NA
Page title component
Installation of thingsboard, an open source IOT platform
R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n
迷你高尔夫球场:伦敦休闲旅游好去处
【JVM调优实战100例】03——JVM堆调优四例
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
Golang concurrent programming goroutine, channel, sync
R语言使用epiDisplay包的cox.display函数获取cox回归模型汇总统计信息(风险率HR、调整风险率及其置信区间、模型系数的t检验的p值、Wald检验的p值和似然比检验的p值)、汇总统计