dsen53898 2015-06-01 04:52
浏览 80
已采纳

去数组的方法

Can I use array and its pointer for Go methods?

I have the following code:

var array = [3]string{"A", "B", "C"}
type arrayTypePt *[3]string
func (m *arrayTypePt) change() { m[1] = "W" }
func main() {
    (arrayTypePt(&array)).changeArray4()
}

But this code: http://play.golang.org/p/mXDEhmA9wk

give me an error of:

invalid receiver type *arrayTypePt (arrayTypePt is a pointer type)
invalid operation: m[1] (type *arrayTypePt does not support indexing)
arrayTypePt(&array).changeArray4 undefined (type arrayTypePt has no field or method changeArray4)

I get the same error when I try this with slice. Why I cannot do this in method?

  • 写回答

2条回答 默认 最新

  • dongzha3058 2015-06-01 05:33
    关注

    The receiver type of a method cannot be a pointer to a pointer, but that is what you wrote:

    func (m *arrayTypePt) change() { m[1] = "W" }
    

    arrayTypePt is already a pointer *[3]string. Quoting from the language specification:

    [The receiver] type must be of the form T or *T (possibly using parentheses) where T is a type name. The type denoted by T is called the receiver base type; it must not be a pointer or interface type and it must be declared in the same package as the method.

    Your 2nd error ("type *arrayTypePt does not support indexing") is also a result of this (m is a pointer to pointer, that's why you can't index it; if it would only be a pointer to array or slice, the pointer indirection would be automatic).

    Your 3rd error is simply a typo, you declared a method named change() and not changeArray4().

    So you should only name the non-pointer array type:

    type arrayType [3]string
    

    And you can declare your array using directly this type:

    var array = arrayType{"A", "B", "C"}
    

    And you can simply call its change() method:

    array.change()
    

    The address of the array will be taken automatically (because the change() method has a pointer receiver but the array variable itself is not a pointer).

    Try it on the Go Playground.

    Notes / Alternatives

    If you would want your array variable to be explicitly [3]string, you could still make it work by converting it to arrayType, setting it to another variable, and change() can be called on this (because being a variable its address can be taken - while the address of a conversion like arrayType(arr) cannot):

    arr2 := [3]string{"A", "B", "C"}
    arr3 := arrayType(arr2)
    arr3.change()
    

    Or if you would declare your variable to be a pointer to type [3]string, you could save the required additional variable (which was only required so we were able to take its address):

    arr4 := new([3]string)
    *arr4 = [3]string{"A", "B", "C"}
    ((*arrayType)(arr4)).change()
    

    Try these variants too on the Go Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题