<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>string on 华说记录我的生活</title>
    <link>http://www.huasay.com/tags/string/</link>
    <description>Recent content in string on 华说记录我的生活</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>zh</language>
    <lastBuildDate>Fri, 04 Nov 2022 23:32:25 +0800</lastBuildDate>
    <atom:link href="http://www.huasay.com/tags/string/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>golang面试 - string与[]byte的区别，如何互相转化</title>
      <link>http://www.huasay.com/2022/11/04/diff_string_byte/</link>
      <pubDate>Fri, 04 Nov 2022 23:32:25 +0800</pubDate>
      <guid>http://www.huasay.com/2022/11/04/diff_string_byte/</guid>
      <description>string是一个8位字节的集合，通常但不一定代表UTF-8编码的文本。string可以为空，但是不能为nil。string的值是不能改变的。string类型本质也是一个结构体，底层本质就是一个byte类型的数组。 byte就是uint8的别名，它是用来区分字节值和8位无符号整数值
string与[]byte的区别 对于[]byte与string而言，两者之间最大的区别就是string的值不能改变。string在底层都是结构体stringStruct{str: str_point, len: str_len}，string结构体的str指针指向的是一个字符常量的地址， 这个地址里面的内容是不可以被改变的，因为它是只读的，但是这个指针可以指向不同的地址。 string的好处：以在不加锁的控制下，多次使用同一字符串，在保证高效共享的情况下而不用担心安全问题
对于[]byte来说，以下操作是可行的：
1 2 b := []byte(&amp;#34;hello world&amp;#34;) b[1] = &amp;#39;i&amp;#39; string修改操作是被禁止的：
1 2 s := &amp;#34;hello world&amp;#34; s[1] = &amp;#39;i&amp;#39; string支持这样的操作：
1 2 s := &amp;#34;hello world&amp;#34; s = &amp;#34;iello world&amp;#34; string与[]byte的互相转化 string的底层数据结构类型：
1 2 3 4 type stringStruct struct { str unsafe.Pointer len int } []byte对应的底层数据结构（本质上是个slice）：
1 2 3 4 5 type slice struct { array unsafe.Pointer len int cap int } 两者对应的结构非常相似。</description>
    </item>
  </channel>
</rss>
