0

I have a struct like this

import (
        "fmt"
)

type Node struct {
        m []string
        o []string
}

func main() {

        var mm = []string{"abc", "def"}
        var oo = []string{"111", "222"}

        var v = Node{m: mm, o: oo}

        for _, d := range []interface{}{v.m, v.o} {
                fmt.Println(d)
        }

}

The output I get is,

[abc def]
[111 222]

My desired output is,

abc,111
def,222

I don't want to use reflect package because, I want to use native go built-ins as much as possible. If it becomes too burdensome I will fall back on reflect package.

NinjaGaiden
  • 3,046
  • 6
  • 28
  • 49

3 Answers3

1

Is there a reason you don't want to use the reflect package? like Iterate through a struct in Go and Iterate Over String Fields in Struct ?

From the former question, it seems like, yeah you can iterate without reflect by iterating through an interface of the fields, https://play.golang.org/p/bPWUII_D7q

package main

import (
    "fmt"
)

type Foo struct {
    num int
    str string
}

func main() {
    foo := &Foo{42, "Hello"} // struct with fields of many types...
    for _, data := range []interface{}{foo.num, foo.str} {
        fmt.Println(data)
    }
}
Community
  • 1
  • 1
Nevermore
  • 7,141
  • 5
  • 42
  • 64
  • Thanks for the suggestion. Why are you dereferencing, &Foo ? According to golang cheatsheet it doesn't show that when declaring variable. Is there a performance gain when you do that? – NinjaGaiden Jan 18 '17 at 17:28
  • 1
    @NinjaGaiden &Foo doesn't dereference, rather it passes the reference of Foo, so foo is actually a pointer, *Foo – Nevermore Jan 18 '17 at 17:49
  • I see, so its more memory efficient. I will use it next time. – NinjaGaiden Jan 18 '17 at 17:52
1

Edit: I just realized my output doesn't match yours, do you want the letters paired with the numbers? If so then you'll need to re-work what you have.

You can use strings.Join and a type switch statement to accomplish this:

https://play.golang.org/p/ygtdxv02uK

package main

import (
    "fmt"
    "strings"
)

type Node struct {
    m []string
    o []string
    p []int
}

func main() {

    var mm = []string{"abc", "def"}
    var oo = []string{"111", "222"}
    var pp = []int{1, 2, 3}

    var v = Node{m: mm, o: oo, p: pp}

    for _, d := range []interface{}{v.m, v.o, v.p} {
        switch d.(type) {
        case []string:
            fmt.Println(strings.Join(d.([]string), ","))
        default:
            fmt.Println(d)
        }
    }

}

The output is:

abc,def
111,222
[1 2 3]
Josh Lubawy
  • 386
  • 1
  • 6
0

Here is what I did,

package main

import (
        "fmt"
)

type Node struct {
        m []string
        o []string
}

func main() {

        var mm = []string{"abc", "def"}
        var oo = []string{"111", "222"}

        var v = Node{m: mm, o: oo}

        for i := range v.m {
                fmt.Println(v.m[i],v.o[i])
        }
}

This gives me my desired output. Just one thing is I have to write v.m so I suppose I should still use reflect package.

Nevermore
  • 7,141
  • 5
  • 42
  • 64
NinjaGaiden
  • 3,046
  • 6
  • 28
  • 49