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

rsudip90
  • 799
  • 1
  • 7
  • 24
  • I saw this: http://stackoverflow.com/questions/7850140/how-do-you-create-a-new-instance-of-a-struct-from-its-type-at-runtime-in-go, but I don't understand it. – rsudip90 Dec 09 '16 at 18:40
  • Are you just looking for a for loop? https://play.golang.org/p/88GiQuQyBP (also, these are slices not arrays, which are different in Go) – JimB Dec 09 '16 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? – rsudip90 Dec 09 '16 at 18:46
  • You mean you want to set the fields via reflection? https://play.golang.org/p/QzHvhMnput – JimB Dec 09 '16 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. – rsudip90 Dec 09 '16 at 19:03
  • This seems like a very awkward data structure to deal with, does it have to be a struct? If the slices of ints are already ordered accordingly, why do they need to be converted to structs at all? If the int's just need names, why not a map? – JimB Dec 09 '16 at 19:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130248/discussion-between-rsudip90-and-jimb). – rsudip90 Dec 09 '16 at 19:10

2 Answers2

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

slomek
  • 4,873
  • 3
  • 17
  • 16
  • 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? – rsudip90 Dec 09 '16 at 18:58
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-

Joao Henrique
  • 547
  • 5
  • 13