将动态数组结构传递给函数 Golang
Passing dynamic array struct to a function Golang
我想创建接受 'dynamic array struct' 的函数并用它来映射来自数据库 *mgodb
的数据
type Cats struct {
Meow string
}
func getCatsPagination() {
mapStructResult("Animality","Cat_Col", Cats)
}
type Dogs struct {
Bark string
}
func getDogsPagination() {
mapStructResult("Animality","Dog_Col", Dogs)
}
func mapStructResult(db string, collection string, model interface{}) {
result := []model{} //gets an error here
err := con.Find(param).Limit(int(limit)).Skip(int(offset)).All(&result) // map any database result to 'any' struct provided
if err != nil {
log.Fatal(err)
}
}
并得到错误 "model is not a type",这是为什么?
任何答案将不胜感激!
将准备好的切片传递给 mapStructResult function
。
type Cats struct {
Meow string
}
func getCatsPagination() {
cats := []Cats{}
mapStructResult("Animality", "Cat_Col", &cats)
}
type Dogs struct {
Bark string
}
func getDogsPagination() {
dogs := []Dogs{}
mapStructResult("Animality", "Dog_Col", &dogs)
}
func mapStructResult(db string, collection string, model interface{}) {
err := con.Find(param).Limit(int(limit)).Skip(int(offset)).All(result) // map any database result to 'any' struct provided
if err != nil {
log.Fatal(err)
}
}
首先go中没有动态结构这个词。结构字段在使用它们之前声明并且不能更改。我们可以使用 bson 类型来处理数据。 bson类型就像map[string]interface{}
用来动态保存数据。
func mapStructResult(db string, collection string, model interface{}) {
var result []bson.M // bson.M works like map[string]interface{}
err := con.Find(param).Limit(int(limit)).Skip(int(offset)).All(&result) // map any database result to 'any' struct provided
if err != nil {
log.Fatal(err)
}
}
有关 bson 类型的更多信息。在这个 godoc 中查找 BSON
我想创建接受 'dynamic array struct' 的函数并用它来映射来自数据库 *mgodb
的数据type Cats struct {
Meow string
}
func getCatsPagination() {
mapStructResult("Animality","Cat_Col", Cats)
}
type Dogs struct {
Bark string
}
func getDogsPagination() {
mapStructResult("Animality","Dog_Col", Dogs)
}
func mapStructResult(db string, collection string, model interface{}) {
result := []model{} //gets an error here
err := con.Find(param).Limit(int(limit)).Skip(int(offset)).All(&result) // map any database result to 'any' struct provided
if err != nil {
log.Fatal(err)
}
}
并得到错误 "model is not a type",这是为什么? 任何答案将不胜感激!
将准备好的切片传递给 mapStructResult function
。
type Cats struct {
Meow string
}
func getCatsPagination() {
cats := []Cats{}
mapStructResult("Animality", "Cat_Col", &cats)
}
type Dogs struct {
Bark string
}
func getDogsPagination() {
dogs := []Dogs{}
mapStructResult("Animality", "Dog_Col", &dogs)
}
func mapStructResult(db string, collection string, model interface{}) {
err := con.Find(param).Limit(int(limit)).Skip(int(offset)).All(result) // map any database result to 'any' struct provided
if err != nil {
log.Fatal(err)
}
}
首先go中没有动态结构这个词。结构字段在使用它们之前声明并且不能更改。我们可以使用 bson 类型来处理数据。 bson类型就像map[string]interface{}
用来动态保存数据。
func mapStructResult(db string, collection string, model interface{}) {
var result []bson.M // bson.M works like map[string]interface{}
err := con.Find(param).Limit(int(limit)).Skip(int(offset)).All(&result) // map any database result to 'any' struct provided
if err != nil {
log.Fatal(err)
}
}
有关 bson 类型的更多信息。在这个 godoc 中查找 BSON