I have arrays of various structs and need to find their length … see example below
package main
import (
"fmt"
)
type X struct {
S string
}
type Y struct {
N int
}
func main() {
type t interface{}
var x, y t
x = []X{{S: "xxx"}, {S: "zzz"}}
y = []Y{{N: 4}, {N: 5}, {N: 3}}
Process(x, y)
}
func Process(i ...interface{}) {
for _, v := range i {
fmt.Println(v)
fmt.Println(len(v))
}
}
package main
import (
"fmt"
"reflect"
)
type X struct {
S string
}
type Y struct {
N int
}
func Process(i ...interface{}) {
for _, v := range i {
s := reflect.ValueOf(v)
if s.Kind() != reflect.Slice {
continue
}
fmt.Println(s.Len(), s)
}
}
func main() {
type t interface{}
var x, y t
x = []X{{S: "xxx"}, {S: "zzz"}}
y = []Y{{N: 4}, {N: 5}, {N: 3}}
Process(x, y)
}