草庐IT

类结构

全部标签

Go 工厂方法返回类型接口(interface),而不是实现接口(interface)的结构

我正在尝试创建一个工厂方法,该方法返回一个实现某个接口(interface)的结构的构造函数。下面是一些示例代码,说明了我正在使用的模式。//GenericInterfacetypeFoointerface{Bar()string}typeFooConstructorfunc(namestring)Foo//AstructthatimplementsFootypeRealFoostruct{Namestring}func(f*RealFoo)Bar()string{returnf.Name}funcNewRealFoo(namestring)Foo{return&RealFoo{Nam

go - 引用另一个结构给出 "undefined"

我有一些非常简单的golang代码:funcmain(){typeconfigstruct{intervalint`mapstructure:"Interval"`statsdPrefixstring`mapstructure:"statsd_prefix"`groups[]group}typegroupstruct{groupstring`mapstructure:"group"`targetPrefixstring`mapstructure:"target_prefix"`targets[]target}}当我运行它时,我得到以下信息:未定义:组我在这里错过了什么?

go - 如何检查嵌套结构中是否存在键

我有以下结构typeGiphyJsonstruct{Typestring`json:"type"`Data[]struct{Imagesstruct{Fixed_heightstruct{Urlstring`json:"url"`}`json:"fixed_height"`}`json:"images"`}`json:"data"`}我需要访问Data[x].Images.Fixed_height.Url。理想情况下,我希望能够在访问Url之前检查每个属性“Data、Images、Fixed_height”是否存在,以确保我没有nil指针异常。由于我对这种语言相当陌生,所以我很好奇这样

Golang return map[string]interface{} 返回变量结构

我需要一个大的结构表,我需要处理返回的结构。packagemainimport("fmt")varfactorymap[string]interface{}=map[string]interface{}{"Date":Date{},"DateTime":DateTime{},}typeDatestruct{yearint//xsd:intYear(e.g.,2009)monthint//xsd:intMonth(1..12)dayint//xsd:intDaynumber}func(d*Date)Init(){d.year=2009d.month=1d.day=1}typeDateTi

arrays - 在结构中写入数组(Golang)

我一直在使用Golang的“测试”包编写测试用例。我遇到过一种情况,我必须将数组和函数指针写入表中。我试过以下:typemyFunctionTypefunc([]float64,[]float64)float64vartestMatrix=[]struct{dataX[]float64dataY[]float64resultfloat64myFunctionmyFunctionType}{{{2,3},{8,7},1,doMagicOne},{2,3},{8,7},1,doMagicTwo},}但每次我最终都会遇到以下错误或其他问题:missingtypeincompositelite

go - 无法设置结构的属性

这个问题在这里已经有了答案:Howtosetandgetfieldsinstruct'smethod(3个答案)关闭5年前。我正在尝试学习如何在go中使用结构。我有以下包裹//src/db/db.gopackagedbtypeDBstruct{pkstring}func(dbDB)SetPk(sstring){db.pk=s}func(dbDB)GetPk()string{returndb.pk}这是我的main.gopackagemainimport("log""db")funcmain(){d:=db.DB{}d.SetPk("HelloWorld")log.Println(d.G

go - 为什么 Go 认为这个结构不遵守这个接口(interface)?

假设我设置了两个Go接口(interface)并按如下方式实现它们:typefooInterfaceinterface{buildBar()barInterface}typebarInterfaceinterface{stuff()}typefooStructstruct{}typebarStructstruct{}func(*fooStruct)buildBar()*barStruct{return&barStruct{}}func(*barStruct)stuff(){}当我尝试将fooStruct分配给fooInterface变量时,我收到以下错误:cannotusefooStr

json - 如何在 REST 端点中将结构作为有效 JSON 返回?

我正在从API获取一些数据,我想在我的Go应用程序的REST端点中提供这些数据。结构是这样的:typeStockstruct{Stockstring`json:"message_id,omitempty"`StockDatamap[string]interface{}`json:"status,omitempty"`}//varStockDataMapStock如果在控制台中打印,它看起来就像它应该的那样。我的Controller是这样的:packagelibimport("net/http""log""encoding/json")funcreturnStocksFromMemory

go - 如何仅将函数的返回值分配给 golang 中的结构字段?

这个问题在这里已经有了答案:Multiplevaluesinsingle-valuecontext(6个答案)关闭4年前。假设我们有这样一个结构:typeDatastruct{aint}而我们想要获取一个返回多个值的函数的单个返回值,并将其赋值给一个Data对象,例如data:=Data{a:strconv.Atoi("1000")}上面的代码不起作用,因为Atoi返回两个值,一个数字和一个错误,所以我们需要以某种方式处理额外的值(错误),但在我的例子中,我不需要评估错误值,它是无法使用_关键字关闭它。在初始化一个结构体时,我想去掉错误的返回值,我该如何实现

go - Go中如何使用reflect递归解析嵌套结构?

我有一个嵌套的三层结构。我想在Go中使用reflect来解析它(使用递归函数)。使用反射和递归函数的原因是可以有不同数量的字段(但前两个字段是固定的)字段类型不固定。嵌套层的数量可以不同(在这个例子中只有三层。它可以更多)这里是一些代码。typeEdgestruct{Uidstring`json:"uid,omitempty"`Namestring`json:"name,omitempty"`ReadArticle`json:"visited,omitempty"`}typeArticlestruct{Uidstring`json:"uid,omitempty"`Namestring`