0

TLDR: Can I register callback functions in golang to get notified if a struct member is changed?


I would like to create a simple two-way-binding between a go server and an angular client. The communication is done via websockets.

Example:

Go:

type SharedType struct {
    A int
    B string
}
sharedType := &SharedType{}
...
sharedType.A = 52

JavaScript:

var sharedType = {A: 0, B: ""};
...
sharedType.A = 52;

Idea:

In both cases, after modifying the values, I want to trigger a custom callback function, send a message via the websocket, and update the value on the client/server side accordingly.

The sent message should only state which value changed (the key / index) and what the new value is. It should also support nested types (structs, that contain other structs) without the need of transmitting everything.

On the client side (angular), I can detect changes of JavaScript objects by registering a callback function.

On the server side (golang), I could create my own map[] and slice[] implementations to trigger callbacks everytime a member is modified (see the Cabinet class in this example: https://appliedgo.net/generics/).

Within these callback-functions, I could then send the modified data to the other side, so two-way binding would be possible for maps and slices.

My Question:

I would like to avoid things like

sharedType.A = 52
sharedType.MemberChanged("A")
// or:
sharedType.Set("A", 52) //.. which is equivalent to map[], just with a predifined set of allowed keys

Is there any way in golang to get informed if a struct member is modified? Or is there any other, generic way for easy two-way binding without huge amounts of boiler-plate code?

maja
  • 17,250
  • 17
  • 82
  • 125

2 Answers2

1

No, it's not possible.


But the real question is: how do you suppose to wield all such magic in your Go program?

Consider what you'd like to have would be indeed possible. Now an innocent assignment

v.A = 42

would—among other things—trigger sending stuff over a websocket connection to the client.

Now what happens if the connection is closed (client disconnected), and the sending fails? What happens if sending fails to complete before a deadline is reached?

OK, suppose you get it at least partially right and actual modification of the local field happens only if sending succeeds. Still, how should sending errors be handled?

Say, what should happen if the third assignment in

 v.A = 42
 v.B = "foo"
 v.C = 1e10-23

fails?

kostix
  • 51,517
  • 14
  • 93
  • 176
  • Interesting questions, but in my use case, changes don't have to be synchronised immediately. The sender could even collect multiple changes (your last example) and send them as a bunch every few seconds. If the connection is lost, it could either be ignored (wait until the connection is reestablished) or the application could reset/abort/panic. From this perspective, one-way-binding should be possible. Two-way-binding is trickier (what happens if both sides manipulate a value), but (imo) is also solveable. – maja Feb 03 '17 at 12:01
  • Context: I have a server application which performs some work on the filesystem, and the web interface should monitor the current state in realtime. Would be nice if I don't have to send updates manually, but just 'monitor' status-structs. – maja Feb 03 '17 at 12:03
0

you could try using server sent events (SSE) to send realtime data to the frontend, while sending a single post request with ur changes. That way you can monitor in the back and send data every second.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 12 '22 at 02:43
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31994327) – MoiioM Jun 14 '22 at 08:56