草庐IT

字段中

全部标签

go - 为结构字段创建函数类型

我可以为结构的字段设置函数类型吗?我需要这样的东西:typeMyStructstruct{Callbackfunc(int)}谢谢 最佳答案 是的,函数在Go中是一流的。您可以声明functiontype任何你可以放置任何其他基本类型(如整数)的地方。 关于go-为结构字段创建函数类型,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/21028760/

Golang 将结构分配给另一个结构字段不起作用

我正在尝试使用Gorp获取所有体育类(class)。体育课有一个类(class)类型,所以我运行第二个查询来检索它们。我得到了所有的类类型,但由于某种原因最终的分配不起作用。packageentitiesimport("fmt""github.com/coopernurse/gorp""time")typeClassstruct{IdintClassTypeIdintClassTypeClassTypeVideoPathstringVideoSizeintDurationfloat64CreatedAttime.TimeVisibleAttime.TimeNoLongerVisible

json - 使用 unmarshal 从响应中获取特定的 JSON 字段

我正在尝试使用他们的开放API获取特定Subreddit的热门提交列表:packagemainimport("encoding/json""fmt""io/ioutil""net/http""os")constapi_endpoint="http://www.reddit.com/r/%s/top.json?t=all&limit=100"funcmain(){//Getconsoleargument'subreddit'andformattheAPIendpointURL.subreddit:=os.Args[1]top_by_subreddit_endpoint:=fmt.Spri

go - 将 map 初始化为 Go 结构中的字段

我有:typeFoostruct{NamestringHandsmap[string]string}aFoo:=Foo{Name:"Henry"Hands:???????}我想为“Hands”设置一些值,但语法不正确。例如,我想使用如下map:"Left":"broken""Right":"missingthumb" 最佳答案 Foo{Name:"Henry",Hands:make(map[string]string),}aFoo.Hands["Left"]="broken"//orjustFoo{Name:"Henry",Hand

go - 使用反射设置指向字段的指针

我有以下结构,并且需要一些字段可以为空,所以我使用指针,主要是为了处理sql空值typeChickenstruct{Idint//NotnullableName*string//canbenullAvgMonthlyEggs*float32//canbenullBirthDate*time.Time//canbenull}所以当我执行以下操作时,我可以看到json结果可以包含值类型的空值,这正是我想要的stringValue:="xx"chicken:=&Chicken{1,&stringValue,nil,nil}chickenJson,_:=json.Marshal(&chicke

pointers - 我应该在 struct 字段上还是在 struct 上创建指针?走

我想知道关于指针的最佳实践是什么。我应该在结构上还是在其字段上定义它们。我虽然定义一个指向结构本身的指针是有意义的,但这里有一个我觉得很有趣的例子。如果所有字段都是指针,为什么我不应该使用指向整个结构的指针来获取每个字段的地址?typeTagstruct{Tag*string`json:"tag,omitempty"`SHA*string`json:"sha,omitempty"`URL*string`json:"url,omitempty"`Message*string`json:"message,omitempty"`Tagger*CommitAuthor`json:"tagger

reflection - Go 反射(reflect)字段索引 - 单个索引与 slice

reflect.StructField有一个类型为[]int的Index字段。关于此的文档有点令人困惑:Index[]int//indexsequenceforType.FieldByIndex当然Type.FieldByIndex也符合预期,对其行为有更清晰的解释://FieldByIndexreturnsthenestedfieldcorresponding//totheindexsequence.ItisequivalenttocallingField//successivelyforeachindexi.//Itpanicsifthetype'sKindisnotStruct.

go - 尝试将 xml.Unmarshal 构造为类型为 map[string]interface{} 的字段时出错

问题是xml.Unmarshal的字段类型为map[string]interface{}的结构将失败并出现错误:unknowntypemap[string]interface{}{XMLName:{Space:Local:myStruct}Name:testMeta:map[]}由于类型为map[string]interface{}的Meta字段是我所能定义的,因此必须动态解码其中的内容。packagemainimport("encoding/xml""fmt")funcmain(){varmyStructMyStruct//metaisasfarasweknow,insidemeta

如果在 const 定义中声明和使用自定义类型,Godoc 不会生成 "const"字段?

我发现Godoc是自动生成文档的好工具。但是我发现,如果我定义一个自定义类型并在我的常量定义中使用它,在godocHTML中,常量将显示在该类型下,而不是在包级别。这是一个简单的例子:const(Info=iotaWarningError)这将在godoc的顶部生成一个“常量”标题。但是,如果我执行以下操作,则不会有ConstantsheadingforthepackagetypeLevelintconst(InfoLevel=iotaWarningError)在godoc输出中,常量将显示在typeLevel下,位于文档中间的某个位置,但不在顶部,也不在包级别。有没有办法使用自定义类

json - 使用 Go (golang) 如何将数据解码为结构,然后从结构中调用特定字段?

我正在尝试执行API请求以从Steam公共(public)API获取一些信息(这主要是为了学习Go以及学习如何处理Json/API请求)到目前为止我已经得到了这段代码:packagemainimport("encoding/json""fmt""io/ioutil""net/http""strconv")typeSteamAPIstruct{APIKeystring}typeGetAppNewsstruct{AppNewsstruct{AppIdint`json:"appid"`NewsItems[]struct{Gidint`json:"gid"`Titlestring`json:"