简体   繁体   English

golang结构的双向绑定

[英]Two-way-binding for golang structs

TLDR: Can I register callback functions in golang to get notified if a struct member is changed? TLDR:如果结构成员发生更改,我可以在 golang 中注册回调函数以获得通知吗?


I would like to create a simple two-way-binding between a go server and an angular client.我想在 go 服务器和 Angular 客户端之间创建一个简单的双向绑定。 The communication is done via websockets.通信是通过 websockets 完成的。

Example:例子:

Go:去:

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

JavaScript: 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.在这两种情况下,修改值后,我想触发自定义回调函数,通过 websocket 发送消息,并相应地更新客户端/服务器端的值。

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.在客户端(角度)上,我可以通过注册回调函数来检测 JavaScript 对象的变化。

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/ ).在服务器端(golang),我可以创建自己的map[]slice[]实现,以在每次修改成员时触发回调(参见本示例中的Cabinet类: 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?如果修改了结构成员,golang有什么方法可以得到通知? Or is there any other, generic way for easy two-way binding without huge amounts of boiler-plate code?或者是否有任何其他通用方法可以在没有大量样板代码的情况下轻松进行双向绑定?

No, it's not possible.不,这是不可能的。


But the real question is: how do you suppose to wield all such magic in your Go program?但真正的问题是:你打算如何在你的 Go 程序中使用所有这些魔法?

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.会——除其他外——触发通过 websocket 连接向客户端发送内容。

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?失败?

you could try using server sent events (SSE) to send realtime data to the frontend, while sending a single post request with ur changes.您可以尝试使用服务器发送事件 (SSE) 将实时数据发送到前端,同时发送带有您更改的单个发布请求。 That way you can monitor in the back and send data every second.这样您就可以在后台监控并每秒发送数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM