What exactly is (nil) doing in this context []string(nil)

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

What exactly is (nil) doing in this context []string(nil)

问题

我看了一下Slice Tricks的代码示例,注意到其中有[]T(nil)的用法。我之前没有见过(nil)这样的写法,也找不到关于它的任何文档,不知道它的用法和具体作用是什么(我知道它是不言自明的,但我想知道它如何与make[]string{}达到相同的效果)。

通过谷歌搜索"golang (nil) slice",我只找到了以下参考信息:

由于切片的零值(nil)的行为类似于零长度的切片,您可以声明一个切片变量,然后在循环中向其追加元素:

但它没有说明它可以在哪里使用,以及具体的作用是什么,比如我是否可以在结构体或其他任何地方使用它?

例如:

package main

import "log"

func main() {
    str := []string{"Hello", "Bye", "Good Day", "????????"}
    cop := append([]string(nil), str...)	

    log.Println(str)
    log.Println(cop)
}

我只是对(nil)的操作方式和它可以操作的对象感兴趣。

比如,[]string(nil)[]string{}是否具有相同的作用,或者它们之间有什么区别?

英文:

I was looking at the Slice Tricks: https://github.com/golang/go/wiki/SliceTricks

and I noticed in their copy example they have []T(nil)

I haven't seen (nil) like this before and I can't find any documentation on using it or what exactly it accomplishes (I know it's self explanatory but I want to know how it acts the same as make or []string{}

the only reference I can find by googling "golang (nil) slice" is

>Since the zero value of a slice (nil) acts like a zero-length slice, you can declare a slice variable and then append to it in a loop:

But it doesn't say where it can be used or exactly what it accomplishes, like can I use this with structs or whatever I want?

e.g.:

package main

import "log"

func main() {
	str := []string{"Hello", "Bye", "Good Day", "????????"}
	cop := append([]string(nil), str...)	

	log.Println(str)
	log.Println(cop)
}

I'm strictly only curious about how (nil) operates, and what it can operate on.

Like does

[]string(nil)

Operate the same as

[]string{}

or what is the difference here

答案1

得分: 12

这是一种类型转换,将nil转换为切片。

以下两种写法是等价的:

var t []string
t := []string(nil)

在这两种情况下,t的类型是[]string,且 t == nil

英文:

This is a type conversion, converting a nil to a slice.

These are equivalent

var t []string
t := []string(nil)

In both cases, t is of type []string, and t == nil

答案2

得分: 0

list := []string(nil) // list是一个空的字符串切片

func append(slice []Type, elems ...Type) []Type

这个函数接受一个字符串切片和一个可变长度的字符串列表,并返回一个将字符串列表追加到末尾的切片。

所以:

cop := append([]string(nil), str...)

将创建一个空的字符串切片,并将字符串列表中的字符串追加到其中,并返回结果。

这个结果切片是一个副本,因为它具有相同的值和相同的顺序。

英文:
list := []string(nil) // list is an empty slice of strings 

func append(slice []Type, elems ...Type) []Type

function takes a slice of strings and a variable length list of strings, and returns a slice where the list of strings is appended to the end.

so:

cop := append([]string(nil), str...) 

will create an empty slice of strings and append the strings in "str" and return the result.

This the result slice is a copy, because it has all of the same values in the same order.

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

发表评论

匿名网友

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

确定