英文:
Trying to understand how to rename a byte array in Go
问题
我正在尝试根据条件逻辑重新分配一个字节数组。我不理解我的选项。以下是代码:
s3Buffer, numBytes, err := DownloadS3File(event.S3Bucket, event.S3ObjectID, session)
header, err = GetHeader(s3Buffer)
var outBuffer []byte
if HeaderIndicatesConversionNeeded(header) {
outBuffer, err = ConvertBuffer(s3Buffer, event.ObjectID)
} else {
// outBuffer = s3Buffer or copy(outBuffer, s3Buffer) or outBuffer = *s3Buffer or ??
}
// 使用 outBuffer...
我希望outBuffer
与s3Buffer
具有相同的类型,即包含我下载的S3对象内容的字节数组。copy
命令似乎不合逻辑,但更直观。我已经阅读了几天的Go教程,但对此问题没有找到明确的解答。我对Go非常陌生,所以我可能在这里做错了什么,我承认这一点。
英文:
I'm trying to reassign a byte array based on conditional logic. I don't understand my options. Here's the code:
s3Buffer, numBytes, err := DownloadS3File(event.S3Bucket, event.S3ObjectID, session)
header, err = GetHeader(s3Buffer)
var outBuffer []byte
if HeaderIndicatesConversionNeeded(header) {
outBuffer, err = ConvertBuffer(s3Buffer, event.ObjectID)
} else {
// outBuffer = s3Buffer or copy(outBuffer, s3Buffer) or outBuffer = *s3Buffer or ??
}
// use outBuffer...
I need to have outBuffer be the same kind of thing as s3Buffer, a byte array containing the contents of the s3 object I've downloaded. The copy command seems illogical, but more straightforward. I've been reading Go tutorials for a few days but I can't find clarity on this. I'm very new to Go so I might be doing something terribly wrong here, I acknowledge.
答案1
得分: 0
outBuffer = s3Buffer
将复制切片头部,但不会复制实际数据。这是最快的方法,完全没问题,只要知道在这个赋值之后,两个变量都将指向相同的数据,所以通过任何一个变量修改数据都会反映在另一个变量上。参考:https://stackoverflow.com/questions/39993688/are-slices-passed-by-value/39993797#39993797
copy()
函数在你想要将一个切片“分离”出来时非常有用。注意,copy()
函数需要你预先分配目标切片的空间,因为它只会复制源切片中可用的数据,并且只会复制到目标切片中能够容纳的空间(它会复制 len(src)
和 len(dst)
中的最小值)。详情请参考:https://stackoverflow.com/questions/30182538/why-cant-i-duplicate-a-slice-with-copy/30182622#30182622
作为 copy()
的替代方案,你可以使用内置的 append()
函数。它可以将元素追加到切片中,这些元素可以是另一个切片的元素;但与 copy()
不同,append()
会在需要时自动处理空间分配。使用方法如下:
outBuffer = append(outBuffer, s3Buffer...)
阅读博文以了解更多关于切片的知识:
Go Slices: usage and internals
Arrays, slices (and strings): The mechanics of 'append'
英文:
outBuffer = s3Buffer
will copy the slice header, but not the actual data. This is the fastest and is totally fine, just know that after this assignment both variables will point to the same data, so modifying the data via any of them would be reflected on the other. See https://stackoverflow.com/questions/39993688/are-slices-passed-by-value/39993797#39993797
copy()
is useful if you want to "detach" one slice from another. Note that copy()
also needs you to preallocate the destination slice, as it copies no more that what's available in the source and what can be copied to the destination (it copies the minimum of len(src)
and len(dst)
). For details, see https://stackoverflow.com/questions/30182538/why-cant-i-duplicate-a-slice-with-copy/30182622#30182622
As an alternative to copy()
, you could use the builtin append()
function. It appends elements to a slice, elements which may be elements of another slice; but–unlike copy()
–append()
takes care of space allocation when needed. It would look like:
outBuffer = append(outBuffer, s3Buffer...)
Read blog posts to learn more about slices:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论