인터페이스를 통한 다형성
그림 4-2처럼 타입이 다른 구조체 세 개를 정의하고, 인터페이스로 아래 구조체 세 개를 같은 방식으로 처리해 보자.

먼저 Cost() float64 메서드 서명을 가진 Coster 인터페이스를 만들고, Coster 인터페이스를 매개변수로 받아 Coster 인터페이스의 Cost()를 출력하는 displayCost() 함수를 만든다.
type Coster interface { Cost() float64 } func displayCost(c Coster) { fmt.Println(“cost: “, c.Cost()) }
Item 타입과 DiscountItem 타입을 정의하고 각 타입에 Cost() float64 메서드를 정의한다.
type Item struct { name string price float64 quantity int } func (t Item) Cost() float64 { return t.price float64(t.quantity) } type DiscountItem struct { Item discountRate float64 } func (t DiscountItem) Cost() float64 { return t.Item.Cost()