func SearchInts(a []int, x int) bool func SearchStrings(a []string, x string) bool
type Slice interface { Len() int Get(int) interface{} } Search(slice Slice, x interface{}) bool
type Ints []int type Strings []string
var strings Strings = []string{"one", "two", "three"} fmt.Println(Search(strings, "one")) // true fmt.Println(Search(strings, "four")) // false var ints Ints = []int{0, 1, 2, 3, 4, 5} fmt.Println(Search(ints, 0)) // true fmt.Println(Search(ints, 10)) // false
// type A struct {} // func (a *A) CallFirst() { fmt.Println("A CallFirst") } // type B struct { A } // func (b *B) CallFirst() { fmt.Println("B CallFirst") } a := new(A) a.CallFirst() // "A CallFirst" b := new(B) b.CallFirst() // "B CallFirst"
// func (a *A) CallSecond() { fmt.Println(a.GetName(), a.GetMessage()) } // func (a *A) GetName() string { return "A" } // func (a *A) GetMessage() string { return "CallSecond" } // func (b *B) GetName() string { return "B" } a.CallSecond() // “A CallSecond” b.CallSecond() // “A CallSecond”, “B CallSecond”
// type SuperType interface { GetName() string GetMessage() string CallSecond() } // func (a *A) allSecond(s SuperType) { fmt.Println(s.GetName(), s.GetMessage()) } // func (a *A) CallSecond() { a.callSecond(a) } // func (b *B) CallSecond() { b.callSecond(b) } a.CallSecond() // “A CallSecond” b.CallSecond() // “B CallSecond”
// type C struct { A } func (c *C) GetName() string { return "C" } func (c *C) CallSecond() { c.callSecond(c) } // , A, B C func DoSomething(a *A) { a.CallSecond() } DoSomething(a) DoSomething(b) // , DoSomething(c) // ,
// , A, B C func DoSomething(s SuperType) { s.CallSecond() } DoSomething(a) // “A CallSecond” DoSomething(b) // “B CallSecond” DoSomething(c) // “C CallSecond”
ArrayList<String> list = new ArrayList<>(); String str = list.get(0);
type List []interface{} list := List{"one", "two", "three"} str := list[0].(string)
// type Getter interface { GetId() int } type Setter interface { SetId(int) } // type Identifier interface { Getter Setter } // type List []Identifier
// Identifier type User struct { id int } // Identifier type Post struct { id int } func assign(setter Setter, i int) func print(getter Getter)
list := List{new(User), new(User), new(Post), new(Post), new(Post)} for i, item := range list { assign(item, i) print(item) }
func Print(str string) error if Print(str) == nil { // } else { // }
switch err := Print(str); err.(type) { case *MissingError: // case *WrongError: // default: // }
Source: https://habr.com/ru/post/282588/
All Articles