on
Go 16. 인터페이스
Go 16. 인터페이스
포함된 인터페이스 type Reader interface { Read() (n int, err error) Close() error } type Writer interface { Write() (n int err error) Close() error } type ReadWriter interface { Reader Writer } 인터페이스가 인터페이스를 포함
Read(), Write(), Close() 메서드를 모두 가져야지 ReadWriter 사용 가능
빈 인터페이스 모든 타입이 가능하다 func PrintVal( v interface{}) { switch t := v.(type) { case int: fmt.Printf("v is int %d
", int(t)) case float64: fmt.Printf("v is float64 %f
", float64(t)) case string: fmt.Printf("v is string %s
", string(t)) default: fmt.Printf("Not supported type %T:%v
", t, t) } } type Student struct { Age int } func main() { PrintVal(10) PrintVal(3.14) PrintVal("Hello") PrintVal(Student{15}) }
인터페이스 기본값 - nil type Attacker interface { Attack() } func main() { var att Attacker att.Attack() // runtime error: nil pointer dereference }
from http://jooseop.tistory.com/146 by ccl(A) rewrite - 2021-09-15 02:27:53