Go- function interface

In Java, everything is Object, the GO, all are type. 
Function func is also a type, just a little bit special: 1, first as func-defined type; 2, before use func must be converted to type.

If func defined type, and implements the interface, for the function interface.
The following students to introduce themselves in English, for example.


main Package 

Import "FMT" 
// define an interface for self-introduction 
type interface introduce { 
    say (String name, int Age ) 
} 

// introduceFunc defined type, followed by all the parameter string, int and returns empty function type are introduceFunc 
type introduceFunc FUNC (String name, int Age ) 
 // introduceFunc type interfaces implemented introduce  FUNC (introduceFunc F) say (String name, int Age ) {  F (name, Age)  } 
 // function chineseSay, with self Chinese introduction, followed by the parameter string, int and returns empty, so is introduceFunc type  FUNC chineseSay (String name, int Age ) {fmt.Printf ( "Hello everybody, my name is% s,% d years old this year \ the n-" , name, age)} // function chineseSay, English self-introduction, followed by the parameter string, int and returns empty, it is introduceFunc type func englishSay (name string, age int) {Fmt.Printf ( "Hi the Everyone, My name IS% S, D and the I'm years Old% \ n-" , name, Age)} // iterate student sets, each student introduced, particularly self-introduction Information ( in Chinese or English) depending on the types of arguments each FUNC (m Map [String] int , introduce H) = m {IF nil && len (m)> 0! {for name, Age: = Range h.say m {( name, Age)}}} FUNC main () {Students.: = Map [String] int { "Hu": 12 is, "Lee": 33 is, "Jim": 31 is } var Handler introduceFunc = chineseSay each (Students., Handler) = Handler englishSay each (Students., Handler) // following compile error, because the function is not converted to // each corresponding type (students, chineseSay)}


Output is as follows:

Hello everybody, my name is lee, 33 years old
Hello everybody, my name is jim, 31 years old
Hello everybody, my name is hu, 12 years old
hi the Everyone, My name IS hu, and the I'm 12 years Old
hi the Everyone, My name Lee IS, and 33 is the I'm years Old
Hi the Everyone, My name IS Jim, and 31 is the I'm years Old

 

If you do not use the example interface function, we have defined two struct: EnglishSay, ChineseSay. Then define the method: say, is to implement the interface introduce. Create a struct instance prior to use. code show as below:

main Package 

Import "FMT" 
// define a self-introduction interface 
type interface introduce2 { 
    say (String name, int Age ) 
} 
type ChineseSay struct { 
 }  FUNC (ChineseSay) say (String name, int Age ) {  fmt.Printf ( " Hello everybody, my name is% s,% d years old this year \ the n-" , name, Age)} EngliseSay of the type struct {} FUNC (EngliseSay) say (String name, int Age ) {fmt.Printf (" hi the Everyone, my name IS S%, and the I'm% D years Old \ n-" , name, Age)} // iterate student sets, each student introduced, particularly self-introduction mode (in Chinese or English) depending on the type of argument func each2 ( Map m [String] int , H introduce2) {m = nil && IF len (m)> 0! {for name, Age: = range m { h.say(name, age) } } } func main() { students := map[string]int{"hu": 12, "lee": 33, "jim": 31} each2(students,ChineseSay{}) each2(students,EngliseSay{}) }

Output:

Hello everybody, my name is jim, 31 years old
Hello everybody, my name is hu, 12 years old
Hello everybody, my name is lee, 33 years old
hi the Everyone, My name IS hu, and the I'm 12 years Old
hi the Everyone, My name Lee IS, and 33 is the I'm years Old
Hi the Everyone, My name IS Jim, and 31 is the I'm years Old

Guess you like

Origin www.cnblogs.com/wanjch/p/11416587.html