英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论