-2

I have a struct like so:

type Docs struct {
    Methods []string
    Route string
}

and then I import that from another file like:

import tc "huru/type-creator"

and use it like so:

type DocsLocal struct {
    tc.Docs
}

I am pretty certain that tc.Docs is just a field in DocsLocal, so this would be a case of composition, right?

if I want to create new instance of DocsLocal, I try this:

d:= DocsLocal{}

but how do I pass in the Methods and Route parameters? If I do this:

methods:= []string{"foo"}
r:="biscuit"
d:= DocsLocal{methods, r}

I get an error:

Cannot use methods (type []string) as type tc.Docs more

So what is the right syntax to use here?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

1 Answers1

1

You can do

d := DocsLocal{tc.Docs{[]string{"foo"}, "biscuit"}}

or

d := DocsLocal{Docs: tc.Docs{[]string{"foo"}, "biscuit"}}

Go Playground

peterm
  • 91,357
  • 15
  • 148
  • 157