尝试取消引用作为指向后端的struct对象的指针的接口,以便可以将值传递给函数

问题描述:

I have an interface that represents a pointer to a struct object after it has been initialized. I want to get access to the Value of the object that the interface references rather than the pointer so that I can pass it by value to a function as the interface type.

Here is an example of my issue: https://play.golang.org/p/_3vKThlj-V

I want to be able to pass val by value to the function EvalTest in my main function so that I can overwrite the pointer to the TestStruct object in another thread without causing pointer dereference issues in another thread.

The problem is that I'm running several thousand go routines against my interface object, but every 50 go routine calls I have to re-initialize my pointer due to a constraint of the system I'm integrating with. This seems to be leading to an inconsistent state where the new struct initialization is not completely finished when the go routines attempt to access it again. So the reason I wanted to pass it by value was so that I don't have to worry about the pointer being swapped out.

Any thoughts on this?

我有一个接口,该接口代表初始化后的struct对象的指针。 我想访问接口引用的对象的值,而不是指针,以便可以将其按值传递给接口类型的函数。 p>

这里是一个示例 我的问题: https://play.golang.org/p/_3vKThlj-V p>

我希望能够按值将val传递给我的主函数中的EvalTest函数,以便可以覆盖另一个线程中的TestStruct对象的指针,而不会导致 p>

问题是我针对接口对象运行了数千个go例程,但是由于限制,我必须每50个go例程调用重新初始化一次指针 我正在集成的系统。 这似乎导致了不一致的状态,当go例程再次尝试访问它时,新的结构初始化未完全完成。 因此,我想按值传递它的原因是,这样我就不必担心指针被换出。 p>

对此有何想法? p>

This was solved by utilizing a channel for my integration api connection and passing a new pointer down the channel whenever it was successfully initialized. There were also some other optimizations that I was able to use to ensure that I was only replacing the pointer in the event of a successful connection. Here is an example of what I used that I got from Paul K in the Go slack channel.

https://play.golang.org/p/AmbsfQOzol