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

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

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

问题

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

type Home struct {
    Bedroom   string
    Bathroom  string
}

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

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

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

func (this *Home) AddBedroomName(value string) {
    this.Bedroom = value
}
func (this *Home) AddBathroomName(value string) {
    this.Bathroom = value
}
英文:

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

type Home struct {
    Bedroom string
    Bathroom string
}

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

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

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.

func (this *Home) AddBedroomName(value string) {
    this.Bedroom = value
}
func (this *Home) AddBathroomName(value string) {
    this.Bathroom = value
}

答案1

得分: 1

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

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

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

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

英文:

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

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

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

答案2

得分: 0

我来帮你翻译一下:

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

func Set(field *string, value string) {
    *field = value
}

home := &Home{"asd", "zxc"}
fmt.Println(home)
Set(&home.Bedroom, "bedroom")
Set(&home.Bathroom, "bathroom")
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:

func Set(field *string, value string) {
	*field = value
}

home := &Home{"asd", "zxc"}
fmt.Println(home)
Set(&home.Bedroom, "bedroom")
Set(&home.Bathroom, "bathroom")
fmt.Println(home)

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

答案3

得分: 0

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

package main

import "fmt"

type Test struct {
    S string
    I int
}

func (t *Test) setField(name string, value interface{}) {
    switch name {
        case "S":
            t.S = value.(string)
        case "I":
            t.I = value.(int)
    }
}

func main() {
    t := &Test{"Hello", 0}
    fmt.Println(t.S, t.I)
    t.setField("S", "Goodbye")
    t.setField("I", 1)
    fmt.Println(t.S, t.I)
}

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

英文:

Use type assertions on an interface value:

package main

import "fmt"

type Test struct {
    S string
    I int
}

func (t *Test) setField(name string, value interface{}) {
    switch name {
        case "S":
            t.S = value.(string)
        case "I":
            t.I = value.(int)
    }
}

func main() {
    t := &Test{"Hello", 0}
    fmt.Println(t.S, t.I)
    t.setField("S", "Goodbye")
    t.setField("I", 1)
    fmt.Println(t.S, t.I)
}

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:

确定