在Go语言中,可以将结构体字段名作为参数传递给函数。

huangapple go评论111阅读模式
英文:

Can you pass a struct fieldname in to a function in golang?

问题

举个例子,假设你有以下这样的结构体,试图尽可能简单地进行示例:

  1. type Home struct {
  2. Bedroom string
  3. Bathroom string
  4. }

你想知道如何将字段名传递给一个函数,或者是否可以传递给函数。

  1. func (this *Home) AddRoomName(fieldname, value string) {
  2. this.fieldname = value
  3. }

显然,这样是行不通的... 我唯一能想到的方法是使用两个函数,但当结构体变得非常庞大并且有很多相似的代码时,这会增加很多额外的代码。

  1. func (this *Home) AddBedroomName(value string) {
  2. this.Bedroom = value
  3. }
  4. func (this *Home) AddBathroomName(value string) {
  5. this.Bathroom = value
  6. }
英文:

Say for example you have something like this, trying to make the example as simple as possible.

  1. type Home struct {
  2. Bedroom string
  3. Bathroom string
  4. }

How do you pass the field name, or can you, to a function?

  1. func (this *Home) AddRoomName(fieldname, value string) {
  2. this.fieldname = value
  3. }

Obviously that does not work... The only way I can see to do this is to use two functions which adds a lot of extra code when the struct gets really big and has a lot of similar code.

  1. func (this *Home) AddBedroomName(value string) {
  2. this.Bedroom = value
  3. }
  4. func (this *Home) AddBathroomName(value string) {
  5. this.Bathroom = value
  6. }

答案1

得分: 1

我现在是你的中文翻译助手,以下是你要翻译的内容:

我所知道的唯一方法是使用反射:

  1. func (this *Home) AddRoomName(fieldname, value string) {
  2. h := reflect.ValueOf(this).Elem()
  3. h.FieldByName(fieldname).Set(reflect.ValueOf(value))
  4. return
  5. }

http://play.golang.org/p/ZvtF_05CE_

英文:

The only way that I am aware of is to use reflection:

  1. func (this *Home) AddRoomName(fieldname, value string) {
  2. h := reflect.ValueOf(this).Elem()
  3. h.FieldByName(fieldname).Set(reflect.ValueOf(value))
  4. return
  5. }

http://play.golang.org/p/ZvtF_05CE_

答案2

得分: 0

我来帮你翻译一下:

我脑海中浮现的另一个想法是这样的,不确定在你的情况下是否有意义:

  1. func Set(field *string, value string) {
  2. *field = value
  3. }
  4. home := &Home{"asd", "zxc"}
  5. fmt.Println(home)
  6. Set(&home.Bedroom, "bedroom")
  7. Set(&home.Bathroom, "bathroom")
  8. fmt.Println(home)

http://play.golang.org/p/VGb69OLX-X

英文:

One more idea that comes to my mind is like this, not sure if it makes sense in your case though:

  1. func Set(field *string, value string) {
  2. *field = value
  3. }
  4. home := &Home{"asd", "zxc"}
  5. fmt.Println(home)
  6. Set(&home.Bedroom, "bedroom")
  7. Set(&home.Bathroom, "bathroom")
  8. fmt.Println(home)

http://play.golang.org/p/VGb69OLX-X

答案3

得分: 0

在接口值上使用类型断言:

  1. package main
  2. import "fmt"
  3. type Test struct {
  4. S string
  5. I int
  6. }
  7. func (t *Test) setField(name string, value interface{}) {
  8. switch name {
  9. case "S":
  10. t.S = value.(string)
  11. case "I":
  12. t.I = value.(int)
  13. }
  14. }
  15. func main() {
  16. t := &Test{"Hello", 0}
  17. fmt.Println(t.S, t.I)
  18. t.setField("S", "Goodbye")
  19. t.setField("I", 1)
  20. fmt.Println(t.S, t.I)
  21. }

在上面的示例中,setField 方法接受一个字符串 name 和一个接口值 value。根据 name 的值,我们使用类型断言将 value 转换为相应的类型,并将其赋值给 Test 结构体的字段。在 main 函数中,我们创建了一个 Test 实例 t,并打印了其初始值。然后,我们调用 setField 方法来修改 t 的字段值,并再次打印结果。

英文:

Use type assertions on an interface value:

  1. package main
  2. import "fmt"
  3. type Test struct {
  4. S string
  5. I int
  6. }
  7. func (t *Test) setField(name string, value interface{}) {
  8. switch name {
  9. case "S":
  10. t.S = value.(string)
  11. case "I":
  12. t.I = value.(int)
  13. }
  14. }
  15. func main() {
  16. t := &Test{"Hello", 0}
  17. fmt.Println(t.S, t.I)
  18. t.setField("S", "Goodbye")
  19. t.setField("I", 1)
  20. fmt.Println(t.S, t.I)
  21. }

huangapple
  • 本文由 发表于 2015年6月12日 01:24:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/30787453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定