multiple-interface-implem
全部标签 我有这样的东西:a:=[]interface{}{}b:=[]interface{}{}typeSstruct{textstring}s:=S{"string"}t:=S{"string"}a=append(a,s)b=append(b,t)a:=append(a,b)a现在我想读取a的元素或元素的元素..但是如何呢? 最佳答案 您想要的称为类型断言。http://golang.org/ref/spec#Type_assertions该页面上的简单示例是:varxinterface{}=7//xhasdynamictypeintan
我有这样的东西:a:=[]interface{}{}b:=[]interface{}{}typeSstruct{textstring}s:=S{"string"}t:=S{"string"}a=append(a,s)b=append(b,t)a:=append(a,b)a现在我想读取a的元素或元素的元素..但是如何呢? 最佳答案 您想要的称为类型断言。http://golang.org/ref/spec#Type_assertions该页面上的简单示例是:varxinterface{}=7//xhasdynamictypeintan
我有一个“主类型”和一个“子类型”嵌入其中。主和子都实现了一个接口(interface)。当我将“主类型”变量分配给接口(interface)类型变量并使用该接口(interface)变量调用实现的方法时,它调用“主类型”实现而不是子类型的实现。我需要调用子类型实现。在GO中可以吗?我认为我的代码设计有问题。这里我给出一个示例代码来描述这个问题。packagemainimport"fmt"typeinterfinterface{typeCheck()}typemaintypestruct{subtype}typesubtypestruct{}func(maintype)typeChec
我有一个“主类型”和一个“子类型”嵌入其中。主和子都实现了一个接口(interface)。当我将“主类型”变量分配给接口(interface)类型变量并使用该接口(interface)变量调用实现的方法时,它调用“主类型”实现而不是子类型的实现。我需要调用子类型实现。在GO中可以吗?我认为我的代码设计有问题。这里我给出一个示例代码来描述这个问题。packagemainimport"fmt"typeinterfinterface{typeCheck()}typemaintypestruct{subtype}typesubtypestruct{}func(maintype)typeChec
考虑以下示例:http://play.golang.org/p/eAot_sVwNDpackagemainimport"fmt"typeIncrementorinterface{Increment()}typeCounterstruct{iintIncrementor}func(c*Counter)Increment(){c.i++}funcmain(){varcIncrementorc=&Counter{}c.Increment()fmt.Println(c)}不幸的是,我需要c=&Counter{}因为Counter.Increment()实现有一个指针接收器,否则c.Increm
考虑以下示例:http://play.golang.org/p/eAot_sVwNDpackagemainimport"fmt"typeIncrementorinterface{Increment()}typeCounterstruct{iintIncrementor}func(c*Counter)Increment(){c.i++}funcmain(){varcIncrementorc=&Counter{}c.Increment()fmt.Println(c)}不幸的是,我需要c=&Counter{}因为Counter.Increment()实现有一个指针接收器,否则c.Increm
我使用本地golang包“容器/列表”来管理堆栈中的inotify事件。当我访问堆栈的项目时,我的类型失败了(我认为)。import("golang.org/x/exp/inotify""container/list""log""fmt")funcmain(){stack:=list.New()watcher,err:=inotify.NewWatcher()iferr!=nil{log.Fatal(err)}err=watcher.Watch(os.Args[1])iferr!=nil{log.Fatal(err)}for{select{caseev:=当我转储ev变量时,对象类型是
我使用本地golang包“容器/列表”来管理堆栈中的inotify事件。当我访问堆栈的项目时,我的类型失败了(我认为)。import("golang.org/x/exp/inotify""container/list""log""fmt")funcmain(){stack:=list.New()watcher,err:=inotify.NewWatcher()iferr!=nil{log.Fatal(err)}err=watcher.Watch(os.Args[1])iferr!=nil{log.Fatal(err)}for{select{caseev:=当我转储ev变量时,对象类型是
我的代码具有以下结构://$GOPATH/experiments/interfaceexport/printer/printer.gopackageprinterimport"fmt"typeResourcePrinterinterface{PrintSomething()}typeJSONPrinterstruct{IsGenericbool}func(printer*JSONPrinter)PrintSomething(){fmt.Println("JSON")}//$GOPATH/experiments/interfaceexporter/printerretriever/pri
我的代码具有以下结构://$GOPATH/experiments/interfaceexport/printer/printer.gopackageprinterimport"fmt"typeResourcePrinterinterface{PrintSomething()}typeJSONPrinterstruct{IsGenericbool}func(printer*JSONPrinter)PrintSomething(){fmt.Println("JSON")}//$GOPATH/experiments/interfaceexporter/printerretriever/pri