草庐IT

树形结构

全部标签

json - 如何创建一个将 json 字符串转换为 golang 结构的通用函数?

首先,我有以下结构:typeUserstruct{UsernamestringPasswordstringFullNamestringMailstring}我尝试创建以下函数:funcFromJson(emptyJsonAbleinterface{},jsonStringstring)interface{}{err:=json.Unmarshal([]byte(jsonString),&emptyJsonAble)iferr!=nil{panic(err)}returnemptyJsonAble}我已经通过以下方式调用了该函数:user:=FromJson(User{},str)但该函

go - 如何在 DataStore 中存储 *time.Time 类型的结构字段的当前时间?

这个问题在这里已经有了答案:Assignvaluereturnedfromfunctiontopointer(1个回答)关闭3年前。根据我的要求,我创建了一个结构为-typeMyRulestruct{CreatedAttime.Time`json:"createdAt"datastore:"createdAt,noindex"`UpdatedAt*time.Time`json:"updatedAt"datastore:"updatedAt,noindex"`}对于createdAt字段,我可以将当​​前时间存储为-MyRule.CreatedAt=time.Now()但是,将当前时间存

go - 如何省略结构字段

如何省略structfiled,在我的例子中,我登录用户并返回带有用户数据和token的响应,但在这种情况下,我需要删除密码字段,我该怎么做?typeLoginFormDatastruct{Loginstring`json:"name"`Passwordstring`json:"password"`}data:=new(LoginFormData)iferr:=c.Bind(data);err!=nil{returnerr}userData:=data//omitpasswordfieldreturnc.JSON(http.StatusOK,map[string]interface{}

Golang 使用反射一个一个地改变一个结构的字段

我有一个这样的结构:typeUserstruct{NamestringUIDintBiostring}我有一个给定的实例化结构,我想遍历该对象中的字段并逐个修改它们。这是我目前的情况user:=User{Name:"Test",UID:1,Bio:"Testbio",}reflectVal:=reflect.ValueOf(user)numFields:=reflectVal.NumField()fori:=0;i但是我收到了这个错误:panic:reflect:reflect.Value.Setusingunaddressablevalue有办法吗? 最佳

go - 在 Gin Framework 中开发的 REST API 的文件夹结构和包命名约定

我是一名NodeJS/PHP开发人员,并且是Go的初学者。在做了一些研究之后,我为我的RESTAPI项目提出了一个像这样的MVC风格的文件夹结构。.+-bin/+-controllers/+-userController/+-userController.go+-models/+-userModel/+-userModel.go+-main.go因此,我可以让我的代码看起来像这样:import"github.com/gin-gonic/gin"import"controllers/userController"router:=gin.Default()router.GET("/user

go - 将值传递给结构时为 "missing type in composite literal"

我在下面这样定义了我的结构:typeS_LoginSuccessedstruct{Codeint`json:"code"`Datastruct{Userstruct{Sexstring`json:"sex"`IsVipbool`json:"is_vip"`Namestring`json:"name"`}`json:"user"`}`json:"data"`Timestampint64`json:"timestamp"`Messagestring`json:"message"`}我用这个来调用它:success_message:=S_LoginSuccessed{123,{{"male"

google-app-engine - 如何使用结构数组实现 google datastore propertyloadsaver

当你有一个结构数组时,你如何为谷歌数据存储实现Load()和Save()?这显然是可能的,但如何实现呢?首先,当您允许数据​​存储本身使用Phone对象列表序列化一个Person时,您可以使用反射来查看它在内部创建了一个列表>*datastore.Entity对象:packagemainimport("fmt""reflect""cloud.google.com/go/datastore")typePhonestruct{TypestringNumberstring}typePersonstruct{NamestringPhone[]Phone}funcmain(){person:=P

go - 如何编辑埋在递归结构中的数组

我有这个结构(注意它是递归的!):typeGroupstruct{NamestringItem[]stringGroups[]Group}我想将一个字符串附加到Item数组,该数组深埋在Group数组的层次结构中。我所掌握的关于这个新项目路径的唯一信息是它所在的组的名称。假设路径是“foo/bar/far”。我想在不覆盖foo、bar或“root”数组的情况下修改bar。基本上,我想编写一个函数来返回一个与原始变量相同但附加了新字符串的新组变量。到目前为止,我已经尝试了以下方法:遍历包含路径的所有组名称的数组,如果它们在当前组中,则将当前组变量设置为该新组。循环完成后,将字符串附加到数

go - 使用反射填充指向结构的指针

我想遍历结构中的字段并提示将字符串值输入字符串字段,对作为结构指针的字段递归执行此操作。目前这是我尝试过的方法,但在尝试在指针的字符串字段中设置此值时出现错误。packagemainimport("fmt""reflect")typeTablestruct{PK*Field}typeFieldstruct{Namestring}funcmain(){PopulateStruct(&Table{})}funcPopulateStruct(ainterface{})interface{}{typeOf:=reflect.TypeOf(a)valueOf:=reflect.ValueOf(a

go - 无法将 yaml 文件解码为结构

我正在尝试将UnmarshalS放入DataCollectionFromYAML----labels:cats,cute,funnyname:"funnycats"url:"http://glorf.com/videos/asfds.com"-labels:cats,ugly,funnyname:"morecats"url:"http://glorf.com/videos/asdfds.com"-labels:dogs,cute,funnyname:"lotsofdogs"url:"http://glorf.com/videos/asasddfds.com"-name:"birddan