0

I have two structs A & B and a nested struct C with A & B, defined as below:

Struct A :

type Source_a struct{
        Sname string
        price float64
        Qty int
    } 

Struct B :

type Source_b struct{
        Sname2 string
        price2 float64
        Qty2 int
        }

Nested Struct C :

type Data struct{

    S_a []Source_a
    S_b []Source_b 
}

I have declared the Source_a & Source_b type var and derived & assigned the values to it from DB. Sample code for Struct A:

//Post DB query
sks := make([]Source_a, 0)
for rows.Next(){
sk := Source_a{}
err := rows.Scan(&sk.Sname, &sk.Uprice, &sk.Qty)
sks = append(sks, sk)

I am having trouble in binding the values of Struct A & B to Struct C. I am trying something but it is throwing errors, pretty sure this is not the correct way:

td := Data{
      S_a: []Source_a{
           Source_a{
           Sname:sks.Sname,
           Uprice:sks.Uprice,
           Qty:sks.Qty,
           },
         },
      S_b: []Source_b{
           Source_b{
           Sname2: sks2.Sname2,
           Uprice2: sks2.Uprice2,
           Qty2: sks2.Qty2,
           },
        },              
    },

Can you please help, I am new to Golang. Let me know if you need clarifications or specifics.

3
  • @mkopriva, Can you please help? Commented Apr 19, 2020 at 23:26
  • 1
    What errors are you getting specifically? Can you edit and include them in the question? Commented Apr 20, 2020 at 0:00
  • I strongly recommend working through the whole Tour of Go once more. Commented Apr 20, 2020 at 5:51

1 Answer 1

2

sks and sks2 seem like slices and you are using them as variables. You can do

td := Data{
      S_a: sks,
      S_b: sks2,             
}
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.