go的一个接口小程序:
package mainimport ( "fmt")type IServer interface { Start() Stop()}type IRestartAble interface { restart()}type ServerA struct {}type ServerB struct {}func (s *ServerA) Start() { fmt.Println("start A")}func (s *ServerA) Stop() { fmt.Println("stop A")}func (s *ServerA) restart() { fmt.Println("restart A")}func (s *ServerB) Start() { fmt.Println("start B")}func (s *ServerB) Stop() { fmt.Println("stop B")}func main() { sa := new(ServerA) sb := new(ServerB) var isa IServer = sa isa.Start() isa.Stop() var irsa IRestartAble = sa irsa.restart() var isb IServer = sb isb.Start() isb.Stop() irsb, ok := isb.(IRestartAble) if ok { irsb.restart() } else { fmt.Println("sb can't restart.") }}