在Golang中,嵌套对象内的对象内的对象。

huangapple go评论72阅读模式
英文:

Object within an Object within an Object Golang

问题

我正在尝试在MongoDB中创建嵌套对象,但没有成功。我想要实现的格式如下:

"Courses":{
  "Date":{
    "CourseName":{
      "hole 1":{
      
      },
      "hole 2":{
      
      },
      ...一直到18个洞
    }
  }
}

我已经尝试并成功获取了course中的date对象,方法如下:

u := req.FormValue("username")
co := req.FormValue("course")
d := req.FormValue("date")

ng := nGame{Username: u, Course: co, Dates: d}
cn := courseName{CName: co}
query := bson.M{"username": u}
update := bson.M{"$push": bson.M{"Course": bson.M{ng.Dates: cn}}}
err = c.Update(query, update)

date对象中包含了course名称,我想要做的是将course名称作为另一个对象,然后再插入hole对象。

我使用的结构体如下:

type (
	nGame struct {
		Username string
		Course   string
		Location string
		Dates    string
	}
)
type (
	courseName struct {
		CName string
	}
)
英文:

I'm attempting to create nested objects in mongoDB with no luck the format I am trying to achieve is as follows

 "Courses":{
    "Date":{
      "CourseName"  :{
                "hole 1"{
                
                }
                "hole 2"{
                
                }
                ...so on until 18 
               }//coursename
             }//date 
          }//courses

I've tried and succeeded with getting the date object within course by doing the following:

u := req.FormValue("username")
co := req.FormValue("course")
d := req.FormValue("date")

ng := nGame{Username: u, Course: co, Dates: d}
cn := courseName{CName: co}
query := bson.M{"username": u}
update := bson.M{"$push": bson.M{"Course": bson.M{ng.Dates: cn}}}  
err = c.Update(query, update)

The date object has the course name inside it what i'm trying to do is make course name another object which then I can insert the hole object.

The Structs i'm using are as follows:

type (
nGame struct {
	Username string
	Course   string
	Location string
	Dates    string
}
)
type (
courseName struct {
	CName string
}
)

答案1

得分: 2

根据你的描述,我理解你的结构可以用Go语言表示如下:

type Hole struct {
    // 这里可以放任何你想要的内容
}

type Course struct {
    Hole1  Hole `json:"hole 1"`
    Hole2  Hole `json:"hole 2"`
    // ...
    Hole18 Hole `json:"hole 18"`
}

type Courses struct {
    // 日期       课程名
    Map map[string]map[string]Course
}

我建议你使用一个包含18个元素的数组来表示你的洞但这取决于你的需求

type Course struct {
    Holes [18]Hole
}

然后你可以实例化一个这样的结构体使用`[18]Hole`数组如果你使用其他实现方式请相应调整):

courses := Courses{
    Map: map[string]map[string]Course{
        "2017-01-01": map[string]Course{
            "Bob's Course": Course{
                Holes: [18]Hole{
                    Hole{
                        // 洞1
                    },
                    Hole{
                        // 洞2
                    },
                    // ...
                },
            },
        },
    },
}

以上是你要翻译的内容。

英文:

Your described structure, as I understand it, can be represented in Go as follows:

type Hole struct {
    // Whatever you want here
}

type Course struct {
    Hole1 Hole `json:"hole 1"`
    Hole2 Hole `json:"hole 2"`
    // ...
    Hole18 Hole `json:"hole 18"`
}

type Courses struct {
    //  Date       CourseName
    map[string]map[string]Course
}

I would suggest, however, using an 18-element array for your holes, but that's up to you:

type Course struct {
    Holes [18]Hole
}

Then you can instantiate one of these trees as (using a [18]Hole array; adjust accordingly if you use a different implementation):

courses := Courses{
    map[string]map[string]Course{
        "2017-01-01": map[string]Course{
            "Bob's Course": Course{
                [18]Hole{
                    Hole{
                      // Hole 1
                    },
                    Hole{
                      // Hole 2
                    },
                    // ..
                },
            },
        },
    },
}

huangapple
  • 本文由 发表于 2017年2月28日 23:51:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/42513223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定