- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试将一部分指针传递给实现接口(interface) LogicAdapter 的结构。这是我的代码:
main.go:
var adapters[]LogicAdapter
adapter1 := &ExampleAdapter{}
fmt.Printf("Addr: %p\n", adapter1)
adapters = append(adapters, adapter1)
bot := ChatterBot{"Charlie", MultiLogicAdapter{adapters}}
bot.getResponse("test", 0)
多适配器逻辑.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\n", &adapter)
}
_ = response
return ""
}
main 的输出是:
Addr: 0x5178f0
Addr: 0xc42000a340
我没有包括 LogicAdapter,因为我认为它没有必要。我不喜欢用太多代码来填充它,但这里是 ChatterBot,如果它能让事情更容易理解(但请记住 bot.getResponse
调用 process
,仅此而已)
chatterbot.go
type ChatterBot struct {
Name string
MultiLogicAdapter
}
func (bot *ChatterBot) getResponse(text string, session int) string {
response := bot.process(text, session)
_ = response
return ""
}
首先,我想到了以这种方式存储指向 LogicAdapter 的指针
var adapters[]*LogicAdapter
但每次我尝试在此处插入 adapter1 指针时,我都会得到:
*LogicAdapter是指向接口(interface)的指针,不是接口(interface)
所以我发现了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).
所以我决定保留 adapters[]
声明,如您在代码中所见。问题在于,除了 adapters
存储指向 adapter1
的指针外,当它在 MultiLogicAdapter
中打印时,它是另一个地址。我知道我将 adapters
作为副本传递给 MultiLogicAdapter
,但该副本将具有对 adapter1
的相同引用。那么发生了什么?
为什么我要遍历指针?因为这个:https://www.goinggo.net/2013/09/iterating-over-slices-in-go.html如果我不这样做,我将创建很多不必要的副本。
最佳答案
在 var adapters []LogicAdapter
中,您有一个接口(interface)数组。接口(interface)本身本质上是一个对象,带有指向您分配给它的对象的指针(带有一些额外信息来跟踪其类型等)。因此,当您向其附加一个 *ExampleAdapter
时,您将创建一个接口(interface)实例,其中包含一个指向 ExampleAdapter
的指针。
稍后,当您执行 for _, adapter := range logic.adapters {...}
时,循环中声明的变量 adapter
将是 slice 中的接口(interface)“对象”。因此,当您使用 &adapter
获取指向它的指针时,您会得到一个指向 interface 的指针,而不是指向接口(interface)包含指向的 ExampleAdapter
的指针。
有关接口(interface)内部结构的更详细说明,请参见示例 https://research.swtch.com/interfaces .
关于pointers - 指针的 slice ,当传递给对象时,得到具有其他地址的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42049875/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!