内容简介:A string contains an array of bytes that, once created, is immutable. By contrast, the elements of a byte slice can be freely modified.Strings can be converted to byte slices and back again:Conceptually, the []byte(s) conversion allocates a new byte array
A string contains an array of bytes that, once created, is immutable. By contrast, the elements of a byte slice can be freely modified.
Strings can be converted to byte slices and back again:
s := “abc”
b := []byte(s)
s2 := string(b)
Conceptually, the []byte(s) conversion allocates a new byte array holding a copy of the bytes of s, and yields a slice that references the entirety of that array. An optimizing compiler may be able to avoid the allocation and copying in some cases, but in general copying is required to ensure that the bytes of s remain unchanged even if those of b are subsequently modified. The onversion from byte slice back to string with string(b) also makes a copy, to ensure immutability of the resulting string s2。
1 在Golang 中string 是不可改变的修改的。
2 []byte转string的时候需要分配一次内存
3 string 转[]byte的时候也需要分配一次内存
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- golang 数据类型之间的转换
- 浅谈 Go 类型转换之间的那些事
- C++ 基类指针和派生类指针之间的转换
- c# – null和datetime之间没有隐式转换
- DataURL 与 File,Blob,canvas 对象之间的互相转换
- c++中几种常见的类型转换。int与string的转换,float与string的转换以及string和long类型之间的相互...
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
计算机网络(第6版)
[美] James F.Kurose、[美] Keith W.Ross / 陈鸣 / 机械工业出版社 / 2014-10 / 79.00元
《计算机网络:自顶向下方法(原书第6版)》第1版于12年前出版,首创采用自顶向下的方法讲解计算机网络的原理和协议,出版以来已被几百所大学和学院选用,是业界最经典的计算机网络教材之一。 《计算机网络:自顶向下方法(原书第6版)》第6版继续保持了以前版本的特色,为计算机网络教学提供了一种新颖和与时俱进的方法,同时也进行了相当多的修订和更新:第1章更多地关注时下,更新了接入网的论述;第2章用pyt......一起来看看 《计算机网络(第6版)》 这本书的介绍吧!