go 언어에서는 함수명이 같을 경우 재선언 오류를 발생시킨다. 다른 메소드를 만들고 오버로드 처리하도록 설계해서 구현해야 한다. 오버로드 처리를 위해 type switch를 사용해서 들어오는 타입에 따라 메소드 호출하는 처리를 사용한다. 함수나 메소드 인자를 다양하게 처리하려면 []interface{} 로 정의해서 다양한 인자를 받아서 오버로딩 처리하면된다. package main import ( "fmt" ) func main() { var p Person p = Person{"dahl"} var s string = "moon" fmt.Println(" overloading ", p.Overload(s)) fmt.Println(" overloading", p.Overload(nil)) } type Person struct { name string } func (this *Person) Overload(args interface{}) string { switch args.(type) { case string: return this.GetName2("moon") case nil: return this.GetName1() default: return " no match" } } func (this *Person) GetName1() string { return this.name } func (this *Person) GetName2(s string) string { return this.name }
Pre-declared Types go 언어에서 빌드인으로 정의된 데이터 타입들. string, number(int, uint, float...), boolean 타입들 Named vs Unnamed Type go언어에서는 사용자 정의(type T_Int int 등)는 기본적으로 named type 으로 인식한다. 그리고 pre-declared types도 named type으로 인식한다. unnamed type은 composited 타입들로 []int, *int 등을 별도로 인식한다. named type 들은 메소드 정의가 가능하지만 unnamed type은 메소드를 만들 수 없다. type Map map[string]string //this is valid func (m Map) Set(key string, value string){ m[key] = value } //this is invalid func (m map[string]string) Set(key string, value string){ m[key] = value } Underlying Type 모든 타입은 underlying type 을 가집니다. 리터럴과 declared type들은 자신의 타입을 underlying type으로 처리한다. 새로 생성되는 타입들에는 기존의 타입들이 있어야 하고 이 타입들도 항상 underlying type 을 가진다. type Map map[string]string type SpecialMap Map SpecialMap과 Map이 underlying type은 map[string]string 이다. Assignability //사용자 정의 타입 type Mystring string // pre-de
댓글
댓글 쓰기