问题标签 [go-reflect]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
6715 浏览

go - 从反射类型创建切片

我正在尝试从reflect.Type. 这就是我到目前为止所拥有的。

但是我收到以下错误,我不确定如何在不将转换硬编码为[]TestStruct.

有没有办法将返回的接口视为一个切片,而不必对从interface{}to的转换进行硬编码[]TestStruct

0 投票
3 回答
2002 浏览

go - 采用不同类型结构的函数

我想知道如何将以下功能简化为一个功能lengthAAlengthBB请注意,在这两个函数中,它只是计算数组的长度,它只是一个示例,可能比这更复杂。理想情况下,我只想要一个用于相同目的的函数(在本例中为len),但可以将不同struct的变量作为变量。

0 投票
2 回答
6319 浏览

variables - Golang 动态变量引用

在 Go 中,我想做这样的事情。我有一个包含许多结构的大对象(使用 Google 的protobuf)。这是一个人为的例子:

我希望能够动态引用事物。例如:

这在 Go 中可能吗?如果是这样,我该怎么做?本质上,我正在创建一个仅包含对象子集的报告,并且我希望能够创建一个映射文件,用户可以在其中将键/值映射在一起,并且我的程序将输出该值。我在 python 中有这个工作,但想尝试使用 Go :)

0 投票
3 回答
11092 浏览

generics - 如何获取地图的键

我有一个名为Keys()获取地图所有键的函数,代码如下:

但我得到了

Go 是否支持泛型,我应该如何修复我的代码?

0 投票
3 回答
2315 浏览

go - 如何reflect.New一个切片和reflect.AppendSlice它到一个源切片

我想点赞并将其附加到另一个切片中reflect.New[]interface{}[]int

我的代码肯定有错误,但不知道如何改正,如何深入理解reflect.Newreflect.AppendSlice用法。

但它给出了一个错误:

戈朗游乐场

0 投票
2 回答
3466 浏览

go - Golang 将列表对象转换为字符串

我有两个结构:

和:

和两片: 我想从
listsA []Aand得到。只包含. 它是一个listsB []BbankCodeslistAlistBbankcodesbankcodes[]string

使用两个功能将变得如此简单。

如何使用一个常用功能?

0 投票
2 回答
402 浏览

pointers - 将 reflect.AppendSlice 的结果赋值给指针

我很难将这段代码(实际上是切片上的左旋转)翻译成更通用的版本,该版本接受interface{}作为输入参数。

我在最后的作业中遇到了麻烦:

错误消息是invalid indirect of a (type {})ais的值interface{},因此*a =将右手值分配给指针指向的空间。我的电话虽然AppendSlice返回。Value我不确定类型断言需要在哪里发生,我想在左侧?

0 投票
1 回答
4823 浏览

go - 如何通过反射从名称中获取类型表示?

有没有办法使用Go 中的反射库从类型的名称到它的类型表示?

我有一个库,用户需要在其中为某些代码生成提供类型表示。我知道这一定是可能的(在某种意义上),因为他们可以创建该类型的变量并调用TypeOf 函数,但是有没有办法绕过这个并从名称中获取表示?

0 投票
0 回答
414 浏览

json - Golang - 使用反射和递归映射 JSON

我正在尝试将 json 数据映射到map[string]interface{}使用 reflect 和 recursion 。该函数按预期工作,但当涉及到嵌套 json 时,它并没有映射所有值。

我有以下 json 结构:

我正在尝试将其映射到字段的字符串和值的接口,其中字段的字符串(如果它是嵌套的)将与点连接.。例如,映射titlemap[title:brand new restarant with changed name]适用于城市nameid它将是map[address.city.id:25 address.city.name:Dhaka]

我写了这个脚本

我的目标是在一个映射中keyvalue其中嵌套 json 的键将是一个字符串,例如parent.child.child.child它是 ajson path并且值将是最后一个孩子的值

最终输出应该是这样的:map[address.city.id:25 address.city.name:Dhaka address.postal:1213 r_id:123456 title:brand new restarant with changed name descripton:here are some new desciption ...]但它会打印map[address.city.id:25 address.city.name:Dhaka r_id:123456 title:brand new restarant with changed name]But not all string field like descriptionand stateof address

我想我需要对类型进行排序然后运行递归函数?

0 投票
1 回答
1258 浏览

go - Two-way-binding for golang structs

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:

JavaScript:

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

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?