Skip to content

Commit

Permalink
[Go] Go Reflect 학습
Browse files Browse the repository at this point in the history
- type, struct, interface
  • Loading branch information
ac2dia committed Aug 12, 2022
1 parent 427b0f2 commit 86d0fa1
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions language/go/# [Go] Go Reflect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# [Go] Go Reflect

## type
- 속성들의 집합을 표현하는 struct
- 행위의 집합을 표현하는 interface

## interface

``` go
type Rectangle struct {
width, height int
}

func (r *Rectangle) Area() int {
return r.width * r.height
}
```
- r 의 경우 "method receiver" 역할을 함
``` go
type Shape interface {
Area() int
Perimeter() float64
}
// --------------------------------
type rectangle struct {
W, H float64
}
type circle struct {
R float64
}
// --------------------------------
func (r rectangle) Area() int {
return r.W * r.H
}
func (r rectangle) Perimeter() floa64 {
return 2 * (r.H + r.W)
}
func (c circle) Area() int {
return c.R * c.R * math.Pi
}
func (c circle) Perimeter() floa64 {
return 2 * c.R * math.Pi
}
// --------------------------------
func Calc(s shape) {
switch s.(type) {
case rectangle:
fmt.Println("I'm a rectangle")
case circle:
fmt.Println("I'm a circle")
}
fmt.Printf("area: %d, perimeter: %.2f", s.Area(), s.Perimeter())
}
```
- 특정 instance 만들기 위한 interface 만드느 경우 반드시 공통으로 구현할 행위(method) 대해서만 선언한다.

## reflect
- 구조체에 대한 정보를 동적으로 알아내는데 사용되는 golang 기능
- 향후 새로운 type 추가되거나, 처리 대상 type 예측할 없는 상황에서 매우 유용합니다!
- 대표적인 예로 "fmt" 패키지가 있습니다.



# Reference
[1] "[golang] interface와 reflect, 그리고 OOP", https://incredible-larva.tistory.com/entry/golang-interface%EC%99%80-reflect-%EA%B7%B8%EB%A6%AC%EA%B3%A0-OOP

0 comments on commit 86d0fa1

Please sign in to comment.