0

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.

Jubilu
  • 21
  • 3
  • Possible duplicate of [Get all fields from an interface](https://stackoverflow.com/questions/39866503/get-all-fields-from-an-interface) – Mikael Jul 03 '19 at 19:55
  • Possible duplicate of [how to write a function to process two types of input data in golang](https://stackoverflow.com/questions/56588337/how-to-write-a-function-to-process-two-types-of-input-data-in-golang) – georgeok Jul 03 '19 at 21:44

1 Answers1

0

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\nV2: %v\n", 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

Giulio Micheloni
  • 1,290
  • 11
  • 25