传递完整的切片范围作为参数 [英] Pass full slice range as parameter

查看:41
本文介绍了传递完整的切片范围作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码,我已经看到一些使用此格式 v [:] 的代码来传递完整切片(不是其一部分)作为参数.

Considering the code below, I have seen some code using this format v[:] for pass full slice (not part of it) as parameter.

v [:] v 之间是否有区别?还是仅仅是开发人员的偏爱?

Is there any difference between v[:] and v? Or it is just a developer preference?

我在下面进行的测试表明我没有区别.我想念什么吗?

The test I did below show me no difference. Am I missing something?

package main

import (
    "fmt"
)

func main() {
    v := []byte {1, 2, 3}

    printSliceInfo(v)
    printSliceInfo(v[:])
}

func printSliceInfo(s []byte) {
    fmt.Printf("Len: %v - Cap: %v - %v\n", len(s), cap(s), s)
}

推荐答案

v 是切片时, v v [之间没有区别:] .当 v 是一个数组时, v [:] 是一个覆盖整个数组的切片.

When v is a slice, there is no difference between v and v[:]. When v is an array, v[:] is a slice covering the entirety of the array.

这篇关于传递完整的切片范围作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆