[Go] gocron源码阅读-go语言中的切片接口和类型综合
发布时间:2021-05-21 06:46:34 所属栏目:大数据 来源: https://www.jb51.cc
导读:// getCommands func getCommands() []cli.Command { command : = cli.Command{ Name: " web " ,Usage: run web server host 0.0.0.0 bind host port,p bind port env,e prod runtime environment,dev|test|prod 上面那些代码非常不容易理解,我们需要把它拆
// getCommands func getCommands() []cli.Command { command := cli.Command{ Name: "web",Usage: run web serverhost0.0.0.0bind hostport,pbind portenv,eprodruntime environment,dev|test|prod Taoshihan{ Name: taoshihanfmt.Println(b.Name) ? 定义一个接口,接口里面有一个成员方法 type Flag interface { GetName() string } ? 定义另一个类型,这个类型刚好就有这个方法,那么就可以认为这个类型实现了接口 type StringFlag struct { } func (t StringFlag) GetName() { return } 这个时候如果定义Flag类型的变量,那么StringFlag也能被赋值过去 var a Flag a = StringFlag{} a.GetName() ? 再回到原代码中的逻辑,如果使用下面这种方式就非常容易理解了 var myflag []Flag myflag = append(myflag,StringFlag{},StringFlag{}) command := Command{ Flags: myflag,} 完整源码: package main import fmt type Flag interface { GetName() } type Command struct { Flags []Flag } type StringFlag struct { } func (t StringFlag) GetName() } type Taoshihan struct { Name } func main() { var a Flag a = StringFlag{} a.GetName() b := Taoshihan{ Name: "taoshihan", } fmt.Println(b.Name) var myflag []Flag myflag = Command{ Flags: myflag,1)">for _,p := range command.Flags { fmt.Println(p.GetName()) } } ? (编辑:北几岛) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |