Golang 高效字符串拼接

2019-11-03  本文已影响0人  已不再更新_转移到qiita

go 用 strings.Builder 代替普通的 + 号拼接, 在本人的项目中性能有 8 ~15倍的提升,之前写的太差了。

golang code

代码是很简单的

package main

func main(){
    var puts fmt.Sprintf

    var builder strings.Builder

    builder.WriteString(puts("0x%x,", blkHash))
    builder.WriteString(puts("%d,", blkHeader.Number))
    builder.WriteString(puts("0x%x,", getFromAddr(tx, big.NewInt(int64(i)))))
    builder.WriteString(puts("0x%x,", txGas))
    builder.WriteString(puts("0x%x,", txGasPrice))
    builder.WriteString(puts("0x%x,", tx.Hash()))
    builder.WriteString(puts("0x%x,", txInput))
    builder.WriteString(puts("%v,", txNonce))
    builder.WriteString(puts("%v,", txTo))
    builder.WriteString(puts("%v,", txValue))

    builder.String()

}


参考:

https://studygolang.com/articles/12796

https://juejin.im/entry/5ad0696751882555784e60d3

上一篇 下一篇

猜你喜欢

热点阅读