Go 구조체 for-loop

1 개요[ | ]

Go 구조체 for-loop
package main

import (
	"fmt"
	"reflect"
)

type Fruit struct {
	Name      string
	Sweetness int
	Price     int
}

func main() {
	fruit1 := Fruit{
		Name:      "Apple",
		Sweetness: 5,
		Price:     100,
	}
	values := reflect.ValueOf(fruit1)
	for i := 0; i < values.NumField(); i++ {
		v := values.Field(i)
		fmt.Println(v)
	}
}
package main

import (
	"fmt"
	"reflect"
)

type Fruit struct {
	Name      string
	Nicknames *string
	Sweetness int
	Price     int
}

func main() {
	fruit1 := Fruit{
		Name:      "Apple",
		Nicknames: nil,
		Sweetness: 0,
		Price:     100,
	}
	values := reflect.ValueOf(fruit1)
	for i := 0; i < values.NumField(); i++ {
		v := values.Field(i)
		fmt.Println(v.Kind(), v.Type(), v.IsZero(), v)
	}
}
package main

import (
	"fmt"
	"reflect"
)

type Fruit struct {
	Name      string
	Sweetness int
	Price     int
}

func main() {
	fruit1 := Fruit{
		Name:      "Apple",
		Sweetness: 5,
		Price:     100,
	}
	types := reflect.TypeOf(fruit1)
	values := reflect.ValueOf(fruit1)
	for i := 0; i < types.NumField(); i++ {
		fmt.Println(types.Field(i).Name, ":", types.Field(i).Type, "=", values.Field(i))
	}
}

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}