草庐IT

struct - golang 从结构中返回第一个字段

我正在尝试返回给定一个属性的所有用户信息,该属性可以是user_id、电子邮件或名称。u:=User{Email:"goda@go.com"})k:=User{Name:"john"}ReturnUserInfo(u)ReturnUserInfo(k)我调用传递一个只有一个字段的用户结构的函数。然后我想在不明确说出电子邮件的情况下解析该字段。最后,我通过传递隐式字段(user_id或电子邮件等)获取用户信息funcReturnUserInfo(uUser)(yUser){//Retrievefirstfieldfromuandsetthemtofieldandvalue.//NOTex

go - 无法在 golang 中将数据从映射正确解码到结构

我目前无法将map中的数据正确解码为结构。以下是代码片段(BriefCodeatplayground):请求您提供在解码数据时获取默认值的原因。packagemainimport("fmt""encoding/json""os")funcmain(){fmt.Println("Hello,playground")typePDPOfferstruct{cart_valueint`json:"cart_value"`discount_amount_defaultint`json:"discount_amount_default"`max_discountstring`json:"max_d

http - 从 golang 中的 reader.io 对象获取属性

我是golang的新手,遇到了一些小问题:当我像这里这样发出http请求时,我得到了remoteApi给我一些响应:res,err:=http.DefaultClient.Do(req)响应的正文包含一些json,例如:{a:'hello'b:5c:[1,2,3]}我需要将“a”的值赋给其他变量。访问res.Body属性之一的最佳方式是什么?我尝试转换为json/string等但没有成功谢谢 最佳答案 像这样的东西应该可以工作:varsstruct{Astring}err:=json.NewDecoder(response.Body

mongodb - 使用 go-gin 和 mgo 从 mongoDB 通过 id 获取民意调查时出错

我如何使用go-gin和MongoDB按id查询民意调查,我尝试了几种方法但我仍然遇到错误(未找到),似乎无法在下面找到我的代码,我的数据库打开数据库:typePollstruct{//IDstring`json:"_id,omitempty"`IDbson.ObjectId`json:"id,omitempty"bson:"_id,omitempty"`Firstnamestring`json:"firstname,omitempty"`Lastnamestring`json:"lastname,omitempty"`Pollstring`json:"poll,omitempty"`

go - 从 Golang 结构生成序列化器

我有一个这样的结构,typeExamplestruct{aintbintcstring}funcCalculate(){obj:=Example{1,2,"lahmacun"}//dosomethinginhere//Ihavetogetthisresultasastring:"[a=1,b=2,c=lahmacun]"//Examplecanbeanything.whichmeanswedontknowanythingaboutstruct.Justweknowitsastruct.}我想做一个序列化器,但我做不到。注意:在nodejs中我们有for...in循环。这很容易。但在go

go - 无法无限期地写入一个 goroutine 并从另一个 goroutine 中读取

我正在学习围棋,但在使用goroutines时遇到了问题。这是我的代码packagemainimport("fmt""sync""time")varcounter=0varwg=sync.WaitGroup{}funcmain(){ticker:=time.NewTicker(time.Second)gofunc(){forrangeticker.C{//wg.Add(1)//deferwg.Done()counter++fmt.Println(counter)//wg.Done()}}()ticker2:=time.NewTicker(time.Second*2)wg.Add(1)g

go - 从结构 slice 动态创建 map[string]struct 的常用函数

我有两个不同的结构,如下所述AabdB和两个过程函数。有什么方法可以让我编写一个通用函数来为struct生成map[string]struct。此外,有什么方法可以使用给定结构名称的反射来创建相同的对象?typeAstruct{namestring//morefields}typeBstruct{namestring//morefields}funcProcessA(input[]A)map[string]A{output:=make(map[string]A)for_,v:=rangeinput{output[v.name]=v}returnoutput}funcProcessB(i

url - 从 URL 中提取值字符串

我想从URL中检索一个值。假设我有一个URL,例如http://myurl.com/theValue1/iWantToRetrieveThis,我想拆分这个值并想检索theValue1和iWantToRetrieveThis。我该怎么做?我试过下面的代码,但它似乎只是检索查询字符串:funcdecodeGetTokenRequest(_context.Context,r*http.Request)(requestinterface{},errerror){fmt.Println("decodinghere",path.Base(r.URL))returngetTokenRequest{

go - 从另一个文件调用函数失败

我有一个文件routes.gopackagemainimport("github.com/gin-gonic/gin""net/http")funcinitializeRoutes(){router.GET("/",func(c*gin.Context){c.HTML(http.StatusOK,"index.html",gin.H{"title":"HomePage",},)})}和main.gopackagemainimport("github.com/gin-gonic/gin")varrouter*gin.Enginefuncmain(){router:=gin.Default

go - 如何从golang中的对象时间获取string或int64?

我从输入源时间戳中获取,然后为该时间戳设置时间“00:00:00”。现在我需要从对象时间获取时间戳timestamp_int:=1532009163time:=time.Date(time.Unix(int64(timestamp_int),0).UTC().Year(),time.Unix(int64(timestamp_int),0).UTC().Month(),time.Unix(int64(timestamp_int),0).UTC().Day(),0,0,0,0,time.Unix(int64(timestamp_int),0).UTC().Location())new_ti