2808 explained code solutions for 75 technologies


golangIterate through struct ("foreach" style)


package main
import "fmt"
import "reflect"

func main() {
  st := struct{name string; age int }{"Joe", 25}
  v := reflect.ValueOf(st)

  for i := 0; i < v.NumField(); i++ {
    fmt.Println(i, v.Field(i))
  }
}ctrl + c
package main

default package declaration

st :=

sample structure

reflect.ValueOf(st)

returns reflection for given structure

for i := 0; i < v.NumField(); i++

iterate over all values of structure