dqe55175 2014-05-12 21:05
浏览 58
已采纳

如何在Golang中为多种类型编写函数?

I'm trying to write a helper function that can take in different custom Types in Golang, but I can't figure out how to do it exactly the way I want. Here's the situation (incidentally, I'm building an API that returns JSON objects implementing HAL protocol. This just means that resources and relationships are returned as links and not simply ID').

I have a number of models in my app, such as Student, Principal, School, etc... Each of these models has many fields, some same, some different. Ideally, I'd like a function that can iterate through the fields of a struct, and change another field in the struct. The big challenge is that these structs can be of type Student, Principal, School, etc...

Models:

type Person struct {
    halgo.Links
    Id        bson.ObjectId 
    Firstname string        
    Lastname  string        
    Email     string        
}

type Student struct {
    Person `bson:",inline"`

    School mgo.DBRef
}

type School struct {
    Id      bson.ObjectId
    Address []string     
    Name    string       
    Description string
}

Then, I'd like a function that can basically take any of these structs, iterate through the fields (using reflect), and do something with each field.

I've tried a function that takes interface{} but the problem is you have to type-assert the argument to have access to any of the fields. And even once you have that, you'll still have to write individual functions for each type/model anyways.:

func GenerateLinksHelper(m interface{}) {
...
}

Ultimately, I'm trying to find a way to write a function that takes in a somewhat arbitrary struct and perform operations on fields that may or may not be there.

  • 写回答

1条回答 默认 最新

  • dongman2721 2014-05-12 21:54
    关注

    I am not sure to understand what you are trying to do, but with reflection, you can see if a field exists or not and then do something with it.

    Example derived from the 'laws of reflection' article (http://blog.golang.org/laws-of-reflection). Play: http://play.golang.org/p/neU3j2MYvz

    package main
    
    import (
        "fmt"
        "reflect"
    )
    
    type T1 struct {
        A int
        B string
    }
    
    type T2 struct {
        A int
    }
    
    func fct(i interface{}) {
        s := reflect.ValueOf(i).Elem()
        typeOfT := s.Type()
        for i := 0; i < s.NumField(); i++ {
            f := s.Field(i)
            if typeOfT.Field(i).Name == "B" {
                fmt.Printf("I am %s and I have a field B: %s
    ", typeOfT.Name(), f.Interface())
            }
        }
    
    }
    
    func main() {
        t1 := T1{23, "skidoo"}
        t2 := T2{23}
        fct(&t1)
        fct(&t2)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!