Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store a struct inside a struct in go?

I have two structure (New and DailyPrediction) with DailyPrediction structure as one of the entity of New structure:

type New struct {
    Id string
    DailyPrediction
}

type DailyPrediction struct {
    Prediction string
}

I am unable to read (or) write the structure new in the datastore. It would be helpful if someone can help me on this.

like image 915
Raj Avatar asked Nov 03 '22 09:11

Raj


1 Answers

It is unclear to me from your question what exactly you are doing with the struct, and in what way it is failing. However, while you are embedding the DailyPrediction struct in your new struct by not giving it a name, it still needs to be initialized. You can see details of how to do that here: http://golang.org/doc/effective_go.html#embedding

For example, in order to initialize your New struct, you may use a line like this:

    n := New{"foo", DailyPrediction{"bar"}}

Could that be what was missing?

like image 88
Derek Avatar answered Nov 09 '22 04:11

Derek