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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。