dongye9182 2019-07-03 19:43
浏览 47

跨多个类型的字段X的通用归因

Currently, I'm working with a set of structures, which define multiple versions of a document. Most of the fields are shared across these different versions, and the actual differences are pretty subtle. What I'm trying to do is refactor the code that parses this document. For that, it would be really good if I could generalize some attributions, where I don't need to know exactly what type I'm working to know that the type will have a specific field.

I've thought about using reflection but if I can avoid it I would.

Lets say we have:

type v1 struct{
  a int
}

and

type v2 struct{
 a int
 b string
}

what I would like to do is something like

func main(){
 var v1 v1
 var v2 v2

 foo(v1)
 foo(v2)
}


func foo (root interface{}){
  root.a = x
}

is it possible? or is there any other way?

edit: 1 - this is not a duplicate of "Get all fields from an interface" as my problem is not to figure out what type I'm dealing with, but to manipulate/treat different types the same way. 2 - this could be a duplicate of "how to write a function to process two types of input data in golang" but the answer provided fails to solve my issue.

  • 写回答

1条回答 默认 最新

  • duanguanya3052 2019-07-03 23:09
    关注

    Use anonymous fields, methods and interfaces. You can define a new struct that contains all the common properties to all other structs. Then, an interface could be defined over this shared struct, that lists all the methods to handle the common properties. Finally, in the previous structs you only need to replace all the common fields with this new interface. At this point, you are able to write generic functions that take the interface as input and handle internal common fields through exposed methods.

    type SettableA interface{
        SetA(int)
        A() int
    }
    
    type sharedA struct {
        a int
    }
    
    func (s *sharedA) SetA(val int) {
        s.a = val
    }
    
    func (s *sharedA) A() int {
        return s.a
    }
    
    type v1 struct {
        SettableA 
    }
    
    type v2 struct {
        SettableA 
        b string
    }
    
    func foo(root SettableA) {
        root.SetA(5)
    }
    
    func main() {
        v1var := v1{
            SettableA: &sharedA{},
        }
        foo(v1var)
        v2var := v2{
            SettableA: &sharedA{},
            b: "test",
        }
        foo(v2var)
        fmt.Printf("V1: %v
    V2: %v
    ", v1var.A(), v2var.A())
    }
    

    Here the interface is not strictly necessary, but it allows you to write a generic function that handles different concrete types.

    https://play.golang.org/p/BgxYrbGE426

    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助