0

I'm trying to create a function that receives multiple types of struct and add those pointer values to another function.

Example:

type Model1 struct {
  Name string
}

type Model2 struct {
  Type bool
}

func MyFunc(value ...interface{}) {
  OtherFunc(value...)
}

func main() {
  MyFunc( new(Model), new(Mode2) );
}

The problem is that OtherFunc only allow &value, &value, etc as parameter. Have some way to pass those values like OtherFunc(&value...)?

2
  • What is the signature of OtherFunc? is it OtherFunc(values ...inteface{}) or something else? Commented Oct 27, 2015 at 17:23
  • I'm using gorm and the signature is func (s *DB) AutoMigrate(values ...interface{}) *DB Commented Oct 27, 2015 at 17:31

1 Answer 1

3

I'm not sure this will solve your problem entirely however, the exact thing you requested is a feature in the language. You just have to use composite-literal syntax for instantiation instead of new. So you could do this to pass pointers; MyFunc( &Model{}, &Mode2{} )

Thing is, you're still going to be dealing with an interface{} within MyFunc so I'm not sure that will just be able to call OtherFunc without some unboxing (would probably be a type assertion if you want to get technical).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.