指针和切片参考类型 - 接收器 [英] Pointer and slice reference type - receiver

查看:220
本文介绍了指针和切片参考类型 - 接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为,一旦它被传唤给我,这会有点显而易见,但是现在,下面不是点击。

 * stack = append(* stack, x)
}

我有一个名为Stack的类型,它是一个空的接口片。鉴于它是空的,Push方法满足界面。考虑到slice是一个引用类型,为什么'stack'接收器不能被值传递?此外,在上面的例子中,接收器作为指针传递,为什么append内置需要再次通过指针传递?



IE为什么不这样做工作,鉴于切片是一个基础数组的引用指针?


$ b

  func(stack Stack )push(x interface {}){
stack = append(stack,x)
}


Go博客上的文章。

它详细解释了发生的事情并完全解答了你的问题。


$ b

传递片段到函数部分:


重要的是要明白,即使slice包含一个指针,它本身也是一个值。在封面下,它是一个保存指针和长度的结构体。它不是一个指向结构体的指针。

因此,您需要一个指针接收器,或者您需要将切片作为值,如果你想用 append 修改它。



如果您只想修改切片的内容,您可以简单地按值切片:


尽管切片标头是按值传递的,但标头包含一个指向数组元素的
指针,所以原始切片标头和
传递给标头的副本该函数描述相同的数组。
因此,当函数返回时,修改后的元素可以是通过原始切片变量看到的


使用追加,您正在修改切片标题。并且

lockquote

因此,如果我们想编写一个修改头部的函数,我们必须将
作为结果参数返回
p>

或者:


函数修改切片头是传递一个指针给它。


你似乎也对指针的使用感到困惑。请参阅规范


对于类型为T的操作数x,地址操作& x生成一个类型为* T的指针给x。




<
$ b


对于指针类型* T的操作数x,指针间接* x表示指向类型T的变量x。


因此,您的示例 * stack = append(* stack,x)并不意味着你正在传递一个指向 append 的指针,恰恰相反 - 你正在引用指针来传递它所指向的值。


I think this will be a bit obvious once it's called out to me, but right now the following isn't clicking.

type Stack []interface{}

func (stack *Stack) Push(x interface{}) {
    *stack = append(*stack, x)
}

I have a type called Stack that is a slice of empty interfaces. Given that it's empty, the Push method satisfies the interface. Given that a slice is a reference type, why can't the 'stack' receiver just be passed in by value? Further more, in the example above the receiver is passed as a pointer why does the append built-in need to be passed by pointer again?

IE why wouldn't this work, given that the slice is a reference pointer to an underlying array?

func (stack Stack) Push(x interface{}) {
    stack = append(stack, x)
}

解决方案

See this article on the Go blog. It explains in detail what is happening and fully answers your question.

From the section Passing slices to functions:

It's important to understand that even though a slice contains a pointer, it is itself a value. Under the covers, it is a struct value holding a pointer and a length. It is not a pointer to a struct.

As a result you either need a pointer receiver or you need to return the slice as a value if you want to modify it with append.

If you just want to modify the contents of a slice you can simply pass the slice by value:

Even though the slice header is passed by value, the header includes a pointer to elements of an array, so both the original slice header and the copy of the header passed to the function describe the same array. Therefore, when the function returns, the modified elements can be seen through the original slice variable.

With append you are modifying the slice header. And

Thus if we want to write a function that modifies the header, we must return it as a result parameter

Or:

Another way to have a function modify the slice header is to pass a pointer to it.

You also seem to have a confusion on the use of pointers. See the spec:

For an operand x of type T, the address operation &x generates a pointer of type *T to x.

And:

For an operand x of pointer type *T, the pointer indirection *x denotes the variable of type T pointed to by x.

Thus your example *stack = append(*stack, x) doesn't mean that you are passing a pointer to append, quite the opposite - you are dereferencing the pointer to pass the value it's pointing to.

这篇关于指针和切片参考类型 - 接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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