1

I have a data with array of array.

data := [][]int{{1,2,3}, {4,5,6}}

and struct

type A struct { I, J, K int }

Now I want to create instance run time for struct A with each array from data, How do I achieve that? If reflect is a way, then tell how?

This is just an example that I want to show you. But let's say if struct A contains 26 fields from A to Z with type of int and I have 100 slices of data from which I can create/init my struct A, then how it could be possible without using dot notation on struct and just looping over field index and assign that field from slice data?

package main

import (
    "fmt"
)

type A struct {
    I, J, K int
}

func main() {
    data := [][]int{
        {1, 2, 3},
        {4, 3, 2},
    }

    var a A
    // create instance of type A with params
    // initialization for each array in data
    fmt.Println(a)
}

Please help me at this link: https://play.golang.org/p/rYuoajn5Ln

7
  • I saw this: stackoverflow.com/questions/7850140/…, but I don't understand it. Commented Dec 9, 2016 at 18:40
  • Are you just looking for a for loop? play.golang.org/p/88GiQuQyBP (also, these are slices not arrays, which are different in Go) Commented Dec 9, 2016 at 18:42
  • Oh Sorry that was mistake, yup they are slices but let's say I have 50 fields in struct and 100 slices which contains my struct data, then how do I achieve this? Commented Dec 9, 2016 at 18:46
  • You mean you want to set the fields via reflection? play.golang.org/p/QzHvhMnput Commented Dec 9, 2016 at 18:57
  • @JimB exactly that is what I want!! thanks bro. But I have a question which one is a good way to do this? via reflection or via hard coded assignment? See reason behind asking this is I have struct with 40 fields and have 1000 slices of slices which contains data for struct and I need good solution. Commented Dec 9, 2016 at 19:03

2 Answers 2

1

I'm not sure if that is what you are looking for, but you can create those objects in a simple loop:

func main() {
    data := [][]int{
        {1, 2, 3},
        {4, 3, 2},
    }

    for _, intArr := range data {
        a := NewA(intArr)
        // a:= A{I: ints[0], J: ints[1], K: ints[2]}

        fmt.Println(a)
    }
}

Full solution available at https://play.golang.org/p/j7fxbmu3jp

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

1 Comment

Thanks for the answer. But is there any other way so that we can directly to do this with field index like for 0 indexed field assign value of 0 indexed data from slice?
0

Here it is, for a more compact version...

Just range over your "data" array of arrays and create a new "A" from each index.

No need to do that in a separate function.

for _, arr := range data {
    a := A{I: arr[0], J: arr[1], K: arr[2]}

    fmt.Println(a)
}

Full solution here: https://play.golang.org/p/jyN7f9c-o-

1 Comment

Thanks for your help, but I achieved this via reflection.

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.