-1

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?

Benji Vesterby
  • 574
  • 5
  • 21
  • 1
    Perhaps provide the example that doesn't work so we can see what you want to do and why. There may be an easier way. Go doesn't have threads but goroutines (none in your example) and you shouldn't have to overwrite pointers. What's the problem you are seeing? Tell us the problem, not your proposed solution. – Kenny Grant Aug 22 '17 at 19:17
  • That's better but best of all would be an example exhibiting the problem. When you say pass by value you presumably mean to copy the instance? If so why not just make a new instance for each request/goroutine? – Kenny Grant Aug 22 '17 at 19:39
  • Your example on the playground does not show anything which would require to get rid of pointer types. Can you provide a real example? – Volker Aug 23 '17 at 04:23
  • I've included an example of something similar to what I did for my solution in my most recent post on this thread. – Benji Vesterby Aug 24 '17 at 17:48

1 Answers1

0

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

Benji Vesterby
  • 574
  • 5
  • 21