1
我有一个结构,我将所有小时和分钟存储到mongodb中。在这种情况下,当我收到修改该值的请求时,我将字符串作为小时和分钟。有没有办法从被给出作为输入将字符串转换为字段名称
字符串中找到字段名你可以看到它here
package main
import "fmt"
type Min struct {
v01 int `bson:"01",json:"01"`
v02 int `bson:"02",json:"02"`
}
type Hour struct {
v01 Min `bson:"01",json:"01"`
v02 Min `bson:"02",json:"02"`
}
func main() {
fmt.Println("Hello, playground")
var h Hour
h.v01.v01 = 1
h.v02.v01 = 2
fmt.Println(h)
h.Set("01", "01", 10)
fmt.Println(h)
}
func (h *Hour) Set(hour string, min string, value int) {
h.v01.v01 = 10 //Here I have hardcoded it
// Is there a way to do this from the given input
// e.g. h.Set("01","01",100)
}
如果您发现,输入为"01","01"
。我想将此输入更改为h.v01.v01
。 Go有可能吗?
注:我在这种情况下目前使用maps
。如果可能的话,我想将其改为结构访问,以便我可以使用goroutine来加速我的程序。目前goroutines不适合写入地图。
够程是不是安全的同时写*什么*。无论您是否有地图或结构,都需要同步访问权限。 – JimB
[GoLang:按名称访问struct属性]的可能重复(http://stackoverflow.com/questions/18930910/golang-access-struct-property-by-name) –