Go之如何實現高效的 [][byte 或 string 鏈接?

文章目錄
  1.多個[]byte 鏈接
  2.多個string相連bash


1. 多個[]byte 鏈接

b1:=[]byte("this is a first string")
b2:=[]byte("this is a second string")
var buffer bytes.Buffer //Buffer是一個實現了讀寫方法的可變大小的字節緩衝
buffer.Write(b1)
buffer.Write(b2)
b3 :=buffer.Bytes()  //獲得了b1+b2的結果
複製代碼

2. 多個string相連

str1:="this is a first string"
str2:="this is a second string"
buffer.WriteString(str1)
buffer.WriteString(str2)
str3 :=buffer.String()  //獲得了str1+str2的結果
複製代碼
相關文章
相關標籤/搜索