删除切片中未分配的(零值)元素。

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

delete unassigned (zero values) in a slice

问题

如何删除所有零值或未分配的值?

这里将一个堆栈跟踪放入了一个切片中。如何删除所有未分配的(零值)?

是否有一些花哨的函数可以切割切片?类似于字符串的子串。

trace := make([]byte, 1024)
runtime.Stack(trace, true)
英文:

How to delete all zero values or unassigned values?

Here a stack trace is put into a slice.. How can all the unassigned (zero values) be deleted?

Is there some fancy function to slice a slice.. Something like substring of a string

trace := make([]byte, 1024)
runtime.Stack(trace, true)

答案1

得分: 1

使用slice表达式来修剪未使用的堆栈缓冲区部分。Stack函数方便地返回写入缓冲区的字节数。

trace := make([]byte, 1024)
n := runtime.Stack(trace, true)
trace = trace[:n]

<kbd>playground链接</kbd>

英文:

Use a slice expression to trim the unused portion of the stack buffer. The Stack function conveniently returns the number of bytes written to the buffer.

    trace := make([]byte, 1024)
    n := runtime.Stack(trace, true)
    trace = trace[:n]

<kbd>playground link</kbd>

huangapple
  • 本文由 发表于 2014年9月27日 08:21:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/26070067.html
匿名

发表评论

匿名网友

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

确定