-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?

0

1 Answer 1

1

You can do

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

or

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

Go Playground

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.