相关疑难解决方法(0)

Go中的标签有什么用途?

Go语言规范中,它提到了标签的简要概述:

字段声明后面可以跟一个可选的字符串文字标记,该标记成为相应字段声明中所有字段的属性.标签通过反射界面可见,但否则将被忽略.

// A struct corresponding to the TimeStamp protocol buffer.
// The tag strings define the protocol buffer field numbers.
struct {
  microsec  uint64 "field 1"
  serverIP6 uint64 "field 2"
  process   string "field 3"
}
Run Code Online (Sandbox Code Playgroud)

这是一个非常简短的解释IMO,我想知道是否有人可以提供我这些标签的用途?

reflection struct go

364
推荐指数
3
解决办法
8万
查看次数

如何在golang和mongodb中通过id找到

我需要使用ObjectIdHex获取值并进行更新并查看结果.我正在使用mongodb和golang.But以下代码无法按预期工作

package main

import (
    "fmt"
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
)

type Person struct {
    Id      bson.ObjectId `json:"id" bson:"_id,omitempty"`
    Name  string
    Phone string
}

func checkError(err error) {
    if err != nil {
        panic(err)
    }
}

const (
    DB_NAME       = "gotest"
    DB_COLLECTION = "pepole_new1"
)

func main() {
    session, err := mgo.Dial("localhost")
    checkError(err)
    defer session.Close()

    session.SetMode(mgo.Monotonic, true)

    c := session.DB(DB_NAME).C(DB_COLLECTION)
    err = c.DropCollection()
    checkError(err)

    ale := Person{Name:"Ale", Phone:"555-5555"}
    cla := Person{Name:"Cla", Phone:"555-1234-2222"}
    kasaun := Person{Name:"kasaun", Phone:"533-12554-2222"}
    chamila := Person{Name:"chamila", Phone:"533-545-6784"}

    fmt.Println("Inserting")
    err …
Run Code Online (Sandbox Code Playgroud)

crud go mongodb

11
推荐指数
2
解决办法
7439
查看次数

在golang中无法通过ObjectId获取mongodb记录

我尝试的ObjectId通过使用下面的代码获取MongoDB的纪录,但继续得到未发现通过err.Error()

以下是我的mongo收集样本

{ "_id" : ObjectId("5a2a75f777e864d018131a59"), "callDate" : "22/12/2017", "time" : "16.25", "callType" : "a", "position" : "aaa", "description" : "aaaaaa", "qty" : 2, "estimatedDuration" : 2.3, "estimatedOvertime" : 3.44, "rate" : 4, "laborExtension" : 3 }
{ "_id" : ObjectId("5a2a75f877e864d018131a5b"), "callDate" : "22/12/2017", "time" : "16.25", "callType" : "a", "position" : "aaa", "description" : "aaaaaa", "qty" : 2, "estimatedDuration" : 2.3, "estimatedOvertime" : 3.44, "rate" : 4, "laborExtension" : 3 }
{ "_id" : ObjectId("5a2a75fa77e864d018131a5d"), "callDate" …
Run Code Online (Sandbox Code Playgroud)

go mongodb bson

6
推荐指数
1
解决办法
2158
查看次数

mgo - 查询性能似乎一直很慢(500-650ms)

我的数据层使用了相当数量的Mongo聚合,平均而言,查询需要500-650毫秒才能返回.我在用mgo.

下面显示了一个示例查询函数,它表示我的大多数查询的外观.

func (r userRepo) GetUserByID(id string) (User, error) {
    info, err := db.Info()
    if err != nil {
        log.Fatal(err)
    }

    session, err := mgo.Dial(info.ConnectionString())
    if err != nil {
        log.Fatal(err)
    }
    defer session.Close()

    var user User
    c := session.DB(info.Db()).C("users")
    o1 := bson.M{"$match": bson.M{"_id": id}}
    o2 := bson.M{"$project": bson.M{
        "first":           "$first",
        "last":            "$last",
        "email":           "$email",
        "fb_id":           "$fb_id",
        "groups":          "$groups",
        "fulfillments":    "$fulfillments",
        "denied_requests": "$denied_requests",
        "invites":         "$invites",
        "requests": bson.M{
            "$filter": bson.M{
                "input": "$requests",
                "as":    "item",
                "cond": bson.M{
                    "$eq": []interface{}{"$$item.active", true}, …
Run Code Online (Sandbox Code Playgroud)

performance go mongodb mgo

5
推荐指数
1
解决办法
1891
查看次数

标签 统计

go ×4

mongodb ×3

bson ×1

crud ×1

mgo ×1

performance ×1

reflection ×1

struct ×1