草庐IT

bottom-type

全部标签

戈朗 : Type Assertion Error issue

我被下面代码片段中的类型断言相关错误困扰了。我不确定我错过了什么。我在以下位置进行类型断言itr=itr.(*DbIterator).Iteratorandkey:=itr.Key().(*Key).Sliceandvalue:=itr.Value().(*Value)。片。我想知道是否有更好的方法来做到这一点,而无需在代码库中到处使用类型断言或更好的设计模式来处理这种情况。该代码片段是更大代码库的一部分。我已经为这个问题提取了最相关的部分。非常感谢这方面的任何帮助。packagerocksdbimport("github.com/tecbot/gorocksdb")func(s*Ro

pointers - 转到错误 : "embedded type cannot be a pointer to interface"

以下代码给出了编译时错误:typeIFileinterface{Read()(nint,errerror)Write()(nint,errerror)}typeTestFilestruct{*IFile}错误:./test.go:18:embeddedtypecannotbeapointertointerface为什么我不能嵌入*IFile? 最佳答案 语言规范不允许。规范中的相关部分:Structtypes:Afielddeclaredwithatypebutnoexplicitfieldnameisananonymousfiel

go - undefined <type float32 has no field or method sum> 错误。如何解决?

这是我的程序。当我运行它时,它给出了以下错误-a.sumundefined(typefloat32hasnofieldormethodsum)packagemainimport("fmt")typeCalculationinterface{operation(input[]float32)}typeAdditionstruct{sumfloat32}func(aAddition)operation(input[]float32){a.sum=input[0]for_,a:=rangeinput[1:]{a.sum+=a}fmt.Println("Sum:",a.sum)}funcmai

Go AST/Types——如何判断错误?

有更好的方法吗?我需要知道v的类型是否是内置的“错误”类型。我觉得应该有一种更简洁的方法来做到这一点:import("go/ast""go/types")funcIsError(vast.Expr,infotypes.Info)bool{t:=info.Types[v]returnt.Type.String()=="error"&&t.Type.Underlying().String()=="interface{Error()string}"} 最佳答案 Typeassertion是检查变量类型的惯用方法。鉴于您正在处理一个AST表

Golang (v1.8) - 错误 : type *mux. Route has no field or method Method

我有一个配备了gorilla工具包的go/golang应用程序。我正在尝试使用gorilla/mux包进行路由。我的路线和错误信息如下。有什么指点吗?路线`r:=mux.NewRouter()r.HandleFunc("/",landing)r.HandleFunc("/contact",contact)r.HandleFunc("/faq",faq)r.HandleFunc("/register",accountsC.New).Method("GET")r.HandleFunc("/register",accountsC.Create).Method("POST")http.List

oop - 需要帮助理解 Go 中的 `map[String]type` 行为

请看这段代码:packageactivityimport("fmt""strconv""time")typeActivitystruct{yearContributionsmap[string]weekContributions}typedayContributionsstruct{Datetime.TimeContributionint}typeweekContributionsstruct{NotationstringAllDays[]dayContributions}func(currentWeekContribution*weekContributions)addContrib

去反射 panic : Call using interface{} as type

我在修改Go中的反射时遇到了一个有趣的场景。call1()有效(返回“hello!”),而call2()因reflect:Callusinginterface{}astype而出现panic字符串.在下面的代码中,call1()和call2()之间的唯一区别是如何创建和初始化inValue。我可以清楚地看到为什么call1()导致inValue成为一个string,而call2()导致inValue成为一个interface,所以我的问题不是为什么我的代码会产生这个,而是:为什么Go在第二种情况下不能执行函数调用?我认为接口(interface)仍然包含成功调用该方法的所有必要信息,因

go - 从 reflect.Type 中删除指针

我有一个reflect.Type,它包含一个指向结构的双指针。我希望能够删除一个间接级别以获得指向该结构的指针。这可能吗?例如,我想这样做:funcfoo(xinterface{}){typ:=reflect.TypeOf(x)fmt.Printf("%v",typ)//prints**FoorealType:=typ.PointsTo()fmt.Printf("%v",typ)//prints*Foo}但据我所知,这个功能并不存在。有一个Indirect函数在Value上运行,但我看不到任何类似的适用于Type的函数。 最佳答案

go - Go 中的 AntLR4 : Invalid type assertion: listener

我从Antlr4语法为Go语言生成了解析器。语法在这里:https://raw.githubusercontent.com/antlr/grammars-v4/master/solidity/Solidity.g4我生成解析器如下:java-jar$PWD/antlr-4.7.1-complete.jar-Dlanguage=Go-oparsersyntax/Solidity.g4生成的solidity_parser.go文件在任何地方都有以下错误listener.(SolidityListener)出现:无效类型断言:listener.(SolidityListener)(非接口(i

go - "graphql.NewList(type)"是做什么的?

事实证明,graphql-go的帮助文档对初学者不友好。我只是想知道.NewList()在以下代码中做了什么:Type:graphql.NewList(types.Workouts) 最佳答案 表示数组类型Listsworkinasimilarway:WecanuseatypemodifiertomarkatypeasaList,whichindicatesthatthisfieldwillreturnanarrayofthattype.Intheschemalanguage,thisisdenotedbywrappingthety