dongqiao1158 2017-02-05 08:13
浏览 32
已采纳

指针切片在传递给对象时会获取具有其他地址的指针

I'm trying to pass a slice of pointers to structs that implement the interface LogicAdapter. Here's my code:

main.go:

var adapters[]LogicAdapter
    adapter1 := &ExampleAdapter{}
    fmt.Printf("Addr: %p
", adapter1)
    adapters = append(adapters, adapter1)
    bot := ChatterBot{"Charlie", MultiLogicAdapter{adapters}}
    bot.getResponse("test", 0)

multiadapterlogic.go:

type MultiLogicAdapter struct {
    adapters []LogicAdapter
}

func (logic *MultiLogicAdapter) process(text string, session int) string {
    //response := logic.adapters[0].process(text, session)
    response := ""
    for _, adapter := range logic.adapters {
        fmt.Printf("Addr: %p
", &adapter)
    }
    _ = response
    return ""
}

The output of main is:

Addr: 0x5178f0

Addr: 0xc42000a340

I didn't include LogicAdapter because I don't think it's necessary. I don't like to fill this with much code, but here's ChatterBot if it makes things easier to understand (but keep in mind that bot.getResponse calls process, that's it)

chatterbot.go

type ChatterBot struct {
    Name string
    MultiLogicAdapter
}

func (bot *ChatterBot) getResponse(text string, session int) string {
    response := bot.process(text, session)
    _ = response
    return ""
}

First of all, I had the idea of storing pointers to LogicAdapters, in this way

var adapters[]*LogicAdapter

but everytime I tried to insert the adapter1 pointer there, I got:

*LogicAdapter is pointer to interface, not interface

so I discovered this and learned this:

When you have a struct implementing an interface, a pointer to that struct implements automatically that interface too. That's why you never have *SomeInterface in the prototype of functions, as this wouldn't add anything to SomeInterface, and you don't need such a type in variable declaration (see this related question).

So I decided to leave the adapters[] declaration as you see in the code. The problem is that besides adapters storing a pointer to adapter1, when it gets printed in MultiLogicAdapter, it's another address. I know that I'm passing adapters to MultiLogicAdapter as a copy, but the copy would have the same references to adapter1. So what's happening?

Why I'm iterating through pointers? Because of this: https://www.goinggo.net/2013/09/iterating-over-slices-in-go.html If I don't, I'll be creating lots of unecessary copies.

  • 写回答

1条回答 默认 最新

  • douyu2817 2017-02-05 19:28
    关注

    In var adapters []LogicAdapter, you have an array of interfaces. The interface is itself essentially an object carrying a pointer to the object you assigned to it (with some extra information to track its type etc.). So when you append an *ExampleAdapter to it, you will create an interface instance holding a pointer to an ExampleAdapter.

    Later, when you do for _, adapter := range logic.adapters {...}, the variable adapter declared in the loop will be a copy of the interface "object" in the slice. So when you take a pointer to it with &adapter you get a pointer to the interface, not to the ExampleAdapter the interface contains a pointer to.

    For a more detailed exposition of the internals of interfaces, see for example https://research.swtch.com/interfaces.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用