Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

... 작업중

소개

예제로 배우는 Go 인터페이스라기 보다는, 예제로 공부하는 Go 인터페이스라고 해야 정확할 것 같다. 나도 배우는 입장이라서...

와우의 캐릭터를 Go 인터페이스로 차근차근 구현해 보려고 한다.

Interface

package main

import (
	"fmt"
)

// 캐릭터 일반 특성
type Human struct {
	name  string
	level int
}

type Race struct {
	name      string
	homeworld string
}

// 종족 특성
type Worgen struct {
	Human
	Race
}

type Pandaren struct {
	Human
	Race
}

// 인터페이스 메서드
type Instance interface {
	Hello()
	Dance()
	Attack(int)
	Information()
}

func (c Worgen) Hello() {
	fmt.Printf("%s : Hello everybody i'm worgen\n", c.Human.name)
}

func (c Pandaren) Hello() {
	fmt.Printf("%s : Good morning, Good afternoon, Gooe evening\n", c.Human.name)
}

func main() {
	Yundream := Worgen{Human{name: "yundream", level: 95},
		Race{name: "Worgen", homeworld: "seoul"}}
	Yundream.Hello()

	Dragona := Pandaren{Human{name: "Dragona", level: 80},
		Race{name: "Pandaren", homeworld: "NewYork"}}
	Dragona.Hello()
}

Empty Interface