草庐IT

类结构

全部标签

json - 动态分配结构

我有一个结构:typepersonstruct{FirstN[10]byteLastName[10]byteAddress[15]bytezip[6]byte}然后我有mapxyz=[01:aaaaaaaaaabbbbbbbbbbccccccccccccccc123456]这张map和我的结构完全一样。基本上,如果我用map中的字符串覆盖我的结构,它就是完全匹配的。我正在尝试使用Marshal获取此数据的JSON字符串。但是为此(据我所知)我需要将映射中的数据更新到结构中,然后将结构指针传递给Marshal但我无法找到任何方法从map中获取带有键“01”(字符串)的数据并用它初始化我的

go - 在动态结构函数 Golang 中修改结构值

我有带setter函数的结构packagemaintypePersonstruct{NamestringAgeint}func(p*Person)SetName(namestring){p.Name=name}funcSomeMethod(humaninterface{}){//Icallthesetterfunctionhere,butdoesn'tseemsexisthuman.SetName("Johnson")}funcmain(){p:=Person{Name:"Musk"}SomeMethod(&p)}报错如下:human.SetNameundefined(typeinte

go - 指针引用未存储在我的 go 程序中的结构中

我是go-lang的新手,我试图弄清楚如何正确地使用结构和依赖注入(inject)。我有点卡住了,因为我无法正确存储对另一个结构的引用。这是我生成CommandController的方法。存在对iris.Application的有效引用。funcProvideCommandController(application*iris.Application,commandRepositorycommand.CommandRepository)(*interfaces.CommandController,error){commandController:=interfaces.CommandC

go - 从函数设置结构字段

我确信有更好的方法可以做到这一点,我知道这很简单,但我是新手,所以请多多包涵。我正在尝试从两个函数(setCalculations和Calculations)设置结构(playersObject)的字段,更具体地说,我传入两个数组(playerData和playerData2从main到这些函数的值,在这些函数中执行计算,并希望返回值以便可以在结构中设置它们。packagemainimport("fmt""os""log""strings""bufio""strconv")typeplayersObjectstruct{firstname,lastnamestringbatting_a

尝试附加相同类型的结构时,Go 返回错误

我在尝试使用追加函数合并两个结构时遇到错误:./test.go:33:18:cannotuseconfigs(typeMapUsers)astypestruct{Userarnstring"yaml:\"userarn\"";Usernamestring"yaml:\"username\"";Groups[]string"yaml:\"groups\""}inappend这是代码。packagemainimport("fmt""gopkg.in/yaml.v2""io/ioutil""os")typeMapUsers[]struct{UserarnstringUsernamestrin

go - 覆盖匿名结构函数

如何覆盖匿名结构函数。为了阐明我的意思,请看下面的代码片段:packagebaseimport("fmt""net/http")typeExecuterinterface{Execute()}typeControllerstruct{}func(self*Controller)Execute(){fmt.Println("HelloController")}func(self*Controller)ServeHTTP(rwhttp.ResponseWriter,r*http.Request){self.Execute()}现在我将Controller结构嵌入到Test结构中,也称为匿名

function - 在单独的 golang 包中声明一个结构不能返回值,但在具体声明时可以

尝试从另一个包中导入一个结构类型,它完美返回,但除非在不使用实例化函数的情况下声明,否则无法找到该结构的值。//Xexecutesandfindsvaluesfine,Zdoesnot.packagemainfuncmain(){x:=&Command{}z:=command.NewCommand()fmt.Println(x.command)fmt.Println(z.command)}packagecommandtypeCommandstruct{//Ourstructureddata/objectforCommandaliasstringcommandstringverboseb

go - 在 Go 中将结构放入 slice 的更好方法是什么

我有funcStruct2slice(somestructManystrings)[]string将字符串结构转换为字符串slice。我相信有更好、更快、更简单的方法来做到这一点,无需importreflect。有吗?typeManystringsstruct{string1stringstring2stringstring3string}funcStruct2slice(somestructManystrings)[]string{v:=reflect.ValueOf(somestruct)values:=make([]string,v.NumField())fori:=0;i

go - 嵌入式结构反对的情况

我们有3种类型:typeAstruct{BC}typeBstruct{xintystring}typeCstruct{zstring}因为匿名字段的字段和方法是promoted,我们可以访问A中的匿名字段B的字段,例如varaAa.x=0B和C嵌入到A中是很明显的,所以我们期望A是相当于:typeDstruct{xintystringzstring}您希望看到什么?我们期望我们可以像这样编写A类型的复合文字:a:=A{x:2}你看到了什么?这个编译错误:unknownfield'x'instructliteraloftypeA我们的问题为什么不能像D那样为A编写复合文字?https:/

将结构/文档插入 mongo 时忽略 JSON 标记(通过 mgo)

这是结构的样子这就是文档在Mongo中的样子。 最佳答案 如果您查看mgo包的文档,您会看到其中的结构使用`bson:"fieldName`而非`json进行注释:"fieldName"`。你可以看到一个例子here这是因为mongo使用bson序列化格式而不是json来通过网络发送结构。bson在存储内容方面与json非常相似,但它是二进制格式,并针对在数据库等存储系统中的使用进行了优化。所以更新你的结构看起来像这样:typeEventstruct{Idstring`bson:"id"`CreationDatetime.Time`