9
reflect package 기본 구조 Moon Yong Joon ( [email protected] )

Reflect package 기본구조이해

Embed Size (px)

Citation preview

Page 1: Reflect package 기본구조이해

reflect package 기본 구조 이해

Moon Yong Joon ([email protected])

Page 2: Reflect package 기본구조이해

interface{} 구조

타입

데이터

인터페이스 타입관리테이블

타입

메소드

인터페이스

interface{}는 모든 타입을 구조하여 타입과 값을 구분하고 타입에 대한 정보를 가지고 있다. 포인터를 받을 경우 데이터 부분에 주소가 저장되면 별도의 데이터영역을 가지게 된다.

원 데이터

포인터로 전달시 별도의 원래의 데이터주소를 관리

Page 3: Reflect package 기본구조이해

interface{} 구조

var inf interface{} inf = xi

fmt.Println(" inf value ", inf) fmt.Println(" inf value canset ", reflect.ValueOf(inf).CanSet())

inf = &xi fmt.Println(" inf pointer ", inf) fmt.Println(" int pointer element ", reflect.ValueOf(inf).Elem()) fmt.Println(" int pointer element value ", reflect.ValueOf(inf).Elem().Int())

fmt.Println(" inf pointer canset ", reflect.ValueOf(inf).Elem().CanSet())

reflect.ValueOf(inf).Elem().SetInt(11) fmt.Println(" inf pointer valeue update ", xi)

//결과값 inf value 33 inf value canset false inf pointer 0x20817c190 int pointer element <int Value> int pointer element value 33 inf pointer canset true inf pointer valeue update 11

인터페이스의 값을 처리시 포인터로 전달해서 실제 데이터를 찾아와서 내부 값을 변경이 가능.

Page 4: Reflect package 기본구조이해

인터페이스 이해하기

인터페이스 처리 함수 정의

func TestTypeOf(any interface{}) interface{} {

return reflect.TypeOf(any)}

인터페이스 처리 함수 정의

fmt.Println(" interface{}", Any(xi))

xii := Any(xi)

//타입캐스팅 int

fmt.Println(" type casting ", xii.(int)-33)

// 결과값

interface{} 33

type casting 0

interface{}는 어떤 타입도 다 받아서 처리가 가능하고 실질적으로 사용시 타입캐스팅을 해서 처리하면 된다.

Page 5: Reflect package 기본구조이해

TypeOf( )함수 처리

타입함수는 인자로 interface{}를 받고 결과로 Type인터페이스를 리턴한다.

reflect.TypeOf(interface{}) Type

var xi int = 33 fmt.Println(" typeof call”,reflect.TypeOf(reflect.TypeOf(xi)))

//결과값 typeof call *reflect.rtype

Page 6: Reflect package 기본구조이해

Type 인터페이스 구조Type 인터페이스는 다양한 타입을 처리하는 메소드를 가지고 있고 이를 내부적으로 구현한 구조체들이 존재

type Type interface{메소드 시그너처}

type rtype struct{필드명} func (r *rtype) 메소드( )

Page 7: Reflect package 기본구조이해

ValueOf( )함수 처리

Value함수는 인자로 interface{}를 받고 결과로 Value구조체를 리턴한다.리턴 Value의 타입을 확인해보면 Value 구조체를 넘긴다.

reflect.ValueOf(interface{}) Value

fmt.Println(" ValueOf call ", reflect.ValueOf(xi)) fmt.Println(" int() ", reflect.ValueOf(xi).Int()) fmt.Println(" ValueOf call Type ", reflect.TypeOf(reflect.ValueOf(xi))) //결과값 ValueOf call <int Value> int() 33 ValueOf call Type reflect.Value

Page 8: Reflect package 기본구조이해

Value 구조체 구조Value 구조체는 다양한 타입을 처리하는 메소드를 가지고 포인터 전달된 경우 값을 갱신할 수 있는 메소드도 제공

type Value struct{필드명} func (r *rtype) 메소드( )

Page 9: Reflect package 기본구조이해

Type/Value구조에 의한 메소드 검색인터페이스 타입관리 테이블 내의 타입에 대한 메소드를 검색하여 출력하기

type typeT int

func (t typeT) Get() int {

return int(t) } func TestTypeOf(any interface{}) interface{} {

return reflect.TypeOf(any) }

var tT typeT tT = 1

fmt.Println(" tT ", tT) fmt.Println(" tT.Get ", tT.Get())

fmt.Println(" Type Method ",reflect.TypeOf(tT).Method(0)) fmt.Println(" Value Method ", reflect.ValueOf(tT).Method(0))

인터페이스 타입관리테이블

타입

메소드