Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
961 views
in Technique[技术] by (71.8m points)

struct - Missing type in composite literal

type A struct {
    B struct {
        Some string
        Len  int
    }
}

Simple question. How to initialize this struct? I would like to do something like this:

a := &A{B:{Some: "xxx", Len: 3}} 

Expectedly I'm getting an error:

missing type in composite literal

Sure, I can create a separated struct B and initialize it this way:

type Btype struct {
    Some string
    Len int
}

type A struct {
    B Btype
}

a := &A{B:Btype{Some: "xxx", Len: 3}}

But it not so useful than the first way. Is there a shortcut to initialize anonymous structure?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.6k users

...