连接 net.Addr 和 []rune

huangapple go评论103阅读模式
英文:

Concatenate net.Addr and []rune

问题

我想要将一个net.Addr的文本表示和一个[]rune使用": "连接起来,得到一个新的[]rune。最符合惯用方式的方法是什么?

我目前有以下代码(为了示例目的,包含了空格和显式类型声明):

var (
    netAddr   net.Addr
    someRunes []rune 
    _         []rune = append(
                  append(
                      []rune(netAddr.String()),
                      []rune(": ")...),
                  someRunes...
              )
)
英文:

I want to join the text representation of a net.Addr, and a []rune using ": ", resulting in a new []rune. What's the most idiomatic way to do this?

I currently have (whitespace and explicit typing for purposes of example):

var (
    netAddr   net.Addr
    someRunes []rune 
    _         []rune = append(
                  append(
                      []rune(netAddr.String()),
                      []rune(": ")...),
                  someRunes...
              )
)

答案1

得分: 3

如果你只是想让你的代码清晰易懂,可以这样做:

[]rune(netAddr.String() + ":" + string(someRunes))

这种方法效率不高,因为其中有一些不必要的复制,但它以一种人类容易阅读的方式传达了逻辑。如果你的性能分析器告诉你这是一个瓶颈,你的多次追加解决方案可能仍然会比必要的复制/分配更多。我会这样做:

sep := []rune(":")
addr := []rune(netAddr.String())
newRuneSlice = make([]rune, 0, len(addr) + len(sep) + len(someRunes))
newRuneSlice = append(newRuneSlice, addr...)
newRuneSlice = append(newRuneSlice, sep...)
newRuneSlice = append(newRuneSlice, someRunes...)

我猜测你的性能分析器不会告诉你这是一个瓶颈,所以一行代码可能是最好的选择。然而,你应该知道这个一行代码只适用于有效的Unicode。如果你的rune切片中有无效的码点或代理对(截至Go 1.1),你将得到错误的rune替换它们。对于大多数情况,这不是一个问题,但值得考虑。

示例:http://play.golang.org/p/AFoBX_2Wem

invalid := utf8.MaxRune + 1
fmt.Println([]rune(string(invalid))[0] == utf8.RuneError) // 输出true

英文:

If you just want your code to be clear and understandable, do something like this:

[]rune(netAddr.String() + ": " + string(someRunes))

This is inefficient since there some unnecessary copies in there, but it conveys the logic in a way a human can read easily. If your profiler later tells you this is a bottleneck, your multiple append solution may still do more copies/allocations than necessary. I would do something like:

sep := []rune(": ")
addr := []rune(netAddr.String())
newRuneSlice = make([]rune, 0, len(addr) + len(sep) + len(someRunes))
newRuneSlice = append(newRuneSlice, addr...)
newRuneSlice = append(newRuneSlice, sep...)
newRuneSlice = append(newRuneSlice, someRunes...)

My guess is that your profiler won't tell you this is a bottleneck so the one liner is most likely best. However, you should be aware that the one liner only works with valid unicode. If you have invalid code points or surrogate pairs (as of Go 1.1) in your rune slice, you will end up with error runes replacing them. For most cases, this isn't a problem but it is worth thinking about.

Example: http://play.golang.org/p/AFoBX_2Wem

invalid := utf8.MaxRune + 1
fmt.Println([]rune(string(invalid))[0] == utf8.RuneError) // prints true

答案2

得分: 2

[]符号表示一个空的切片,rune()函数将字符串转换为Unicode字符的切片。netAddr.String()返回网络地址的字符串表示形式,+符号用于连接字符串,": "表示冒号和空格。

英文:
[]rune(netAddr.String()+": ")

Be clear and consise first, optimize later if there's a problem.

huangapple
  • 本文由 发表于 2013年5月31日 23:19:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/16860654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定