草庐IT

data_struct

全部标签

struct - 允许任何类型的 slice 作为参数

我是Go的新手(来自python),我在这里遇到了一些困难。我试图允许任何类型的slice进入我的结构/函数,它只包含该slice长度的计数。import"go/types"typeResponsestruct{Countint`json:"count"`Results[]types.Struct`json:"results`}funcNewResponse(results[]types.Struct)(r*Response){r.Count=len(results)r.Results=resultsreturn} 最佳答案 您可以

data-structures - 在 Go 中存储和迭代命名嵌套数据结构的惯用方法?

我的问题分为两个:(1)为下面的taskList存储数据的最佳方式是什么,以及(2)迭代这种结构的最佳方式是什么?我想要命名task1因为它们是独特的任务并且不应该有ID冲突。我想要单独命名为subtask0,因为它们是具有不同要求的独特任务。下面是我意图的伪围棋表示:packagemainimport"fmt"fnmain(){consttaskList:={"task1":{"subtask0":"api.example.com/stuff/""subtask1":"api.example.com/stuff/""subtask2":"api.example.com/stuff/"

go - 在 struct golang 中打印一个 struct

我想打印结构中的某个项目,即结构中的某个项目。示例:假装我已经制作了一个蓝图结构,并且正在制作一个新的假设我有一个myshapes结构,其中包含标题、圆圈和正方形的数量car:=blueprints{dots:5,lines:25,shapes:[]myshapes{myshapes{title:"car#1",circles:5,squares:7,},myshapes{title:"car#2",circles:2,squares:14,},}如何打印:title:"car#1"circles:5squares:7title:"car#2"circles:2squares:14

math - 戈朗 : arithmetic operators on structs

有没有办法在结构之间定义算术运算符?我正在使用小数包来处理固定的小数位并避免四舍五入的float错误。Ir定义操作调用函数,如mul、add、sub等。我喜欢像使用float一样使用该结构:6/2,而不是decimal.newfromfloat(6).div(newfromfloat(2))我希望找到一些接口(interface)来实现,让我大声做那种操作,或者可能是某种gettersetter来处理底层的值......有什么想法吗? 最佳答案 不,您不能在Go中重载运算符。有一个关于它的FAQ条目:WhydoesGonotsupp

关于struct的golang语法问题

这里有一些代码,但是太长了而且没有必要。有时我需要写一些东西到mysql,有一些类似的表格。我一直在尝试使用interface{},但它更复杂。有什么办法可以让它更短吗?typeOnestruct{IdintNameStringStatusboolDevtypestring...Createdtime.Time}typeTwostruct{IdintNameStringStatusboolDevtypestring...Createdtime.Time}typeThreestruct{IdintNameStringStatusboolDevtypestring...Createdtim

curl - Beego 如何访问使用 multipart/form-data header 提交的参数?

我遇到了如下问题:当我向我的beego应用程序发出curl请求时curlhttp://localhost:8080/controller/path-XPOST-H'Content-Type:multipart/form-data;charset=UTF-8'-F“file=@file.csv;filename=file.csv”-F“name=first”我想从我的Controller访问name参数,但是当我尝试时func(c*Controller)Path(){...varnamestringc.Ctx.Input.Bind(&name,"name")//orI'vetried'n

json - 戈朗 : Multiple structs marshall issue: json format

对于以下代码,我得到错误:typeAstruct{B_j[]B`json:"A"`}typeBstruct{XstringYstring}funcmain(){xmlFile,_:=os.Open("test.xml")b,_:=ioutil.ReadAll(xmlFile)vartrooterr2:=xml.Unmarshal(b,&rpc)iferr2!=nil{fmt.Printf("error:%v",err2)return}for_,name:=ranget.name{t:=A{B_j:[]B{X:name.text,Y:name.type}}//line:#25s,_:=j

go - Group 没有实现 Data(FooMethod 方法有指针接收器)

这是我的代码:packagemainimport"fmt"typeGroupstruct{}func(g*Group)FooMethod()string{return"foo"}typeDatainterface{FooMethod()string}funcNewJsonResponse(dData)Data{returnd}funcmain(){vargGroupjson:=NewJsonResponse(g)fmt.Println("vim-go")}但没有像我预期的那样工作。$gobuildmain.go#command-line-arguments./main.go:22:ca

inheritance - 在 Go 中实现 Struct 抽象的正确方法是什么?

我在理解Go的结构继承时遇到了一些问题。我正在尝试对对象类型做一些抽象。请参阅下面的示例代码:packagemaintypeAnimalstruct{}typeDogstruct{AnimalColorstring}typePersonstruct{NamestringAgeintPet*Animal}funcmain(){dog:=&Dog{Color:"brown"}tom:=&Person{Name:"Tom",Age:13,Pet:dog}}这会导致编译错误:cannotusedog(type*Dog)astype*Animalinfieldvalue进行这样的抽象的正确方法是

go - 如何访问 Structs 内部的 map ?不能获取 g.vertexes[base] 的地址

考虑以下问题。我有两个结构,Graph和Vertexpackagemainimport("github.com/shopspring/decimal")typeGraphstruct{vertexesmap[string]Vertex}typeVertexstruct{keystringedgesmap[string]decimal.Decimal}和Vertex的引用接收器func(v*Vertex)Edge(tstring,wdecimal.Decimal){v.edges[t]=w}我想在不同时间更新Graph结构内Vertex.edges映射的值。我最初尝试了这段来自Pytho