據我目前所知,去沒有一種方法來連接字符串/字節到一個結果字符串,而不至少做兩個/副本。
由於所有字符串值都是常量,所以您當前必須增長[]字節,那麼您必須使用字符串內建語言來讓語言創建一個'祝福'字符串對象,它將複製緩衝區,可以引用支持[]字節的地址。
如果一個[]字節是合適的,那麼你可以在字節上獲得非常小的優勢。通過進行一次分配和執行副本來調用你自己的聯接功能。
package main
import (
"fmt"
)
func main() {
m := make(map[int]string)
m[1] = "a" ; m[2] = "b" ; m[3] = "c" ; m[4] = "d"
ip := 0
/* If the elements of m are not all of fixed length you must use a method like this;
* in that case also consider:
* bytes.Join() and/or
* strings.Join()
* They are likely preferable for maintainability over small performance change.
for _, v := range m {
ip += len(v)
}
*/
ip = len(m) * 1 // length of elements in m
r := make([]byte, ip, ip)
ip = 0
for _, v := range m {
ip += copy(r[ip:], v)
}
// r (return value) is currently a []byte, it mostly differs from 'string'
// in that it can be grown and has a different default fmt method.
fmt.Printf("%s\n", r)
}
,而不是「_」在你的for循環,IDX稱它爲和溝IDX +業務 –
不,他不能,當你在範圍內的地圖,它返回鍵,值不是指數值。在他的例子中,他使用1作爲第一個鍵,並且會使分片v中的索引不正確,因爲開始索引將是1而不是0,並且當它到達4時它將超出範圍。 https://play.golang.org/p/X8_SbgxK4VX – Popmedic