
我正在查看 sha1 相关代码 https://cs.opensource.google/go/go/+/refs/tags/go1.21.5:src/crypto/sha1/sha1.go;l=146-152
尤其是这一行 append(in, hash[:]...)
我不确定为什么使用 hash[:]...,而 hash... 似乎就足够了。
这是一段测试代码 https://go.dev/play/p/DaIa0X4KyeD
func main() {
s := make([]int, 2, 10)
s[0] = 1
s[1] = 2
d := []int{88}
d = append(d, s[:]...) // d = append(d, s...) seems to work the same
fmt.Printf("d is: (%v)\n", d)
fmt.Printf("d len is: (%v)\n", len(d))
fmt.Printf("d cap is: (%v)\n", cap(d))
}所以我的问题是 [:] 对于切片来说有什么意义?谢谢!
hash 是一个数组(类型为 [Size]byte),而不是切片。 hash[:] 是一个切片 — 相当于 hash[0:len(hash)]。 ... 表示法需要一个切片,因此它应用于切片 hash[:] 而不是数组 hash。
以上就是Go 切片的用法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号