struct中的slice字段被最后一个值覆盖

时间:2020-08-04 14:52:53

标签: go

我有一个功能,该功能基本上是从mongoDB数据库中收集用户列表,然后将这些用户存储在结构中。

// GetUsers gets all local users from a MongoDB
func GetUsers() {
    var decode structs.User
    logging.Info("Gathering Users from DB")
    collection := mongoclient.Database.Collection("LocalUsers")
    cur, err := collection.Find(context.TODO(), bson.D{})
    if err != nil {
        logging.Fatal(fmt.Sprintf("In users - Getting data from MongoDB: %s", err))
    }

    for cur.Next(context.TODO()) {
        // create a value into which the single document can be decoded
        err := cur.Decode(&decode)
        if err != nil {
            logging.Fatal(fmt.Sprintf("In users - Error decoding results: %s", err))
        }
        // if there is nothing in the database dont try create a User
        if decode.Username != "" {
            structs.LocalUsers[decode.Username] = decode
            fmt.Printf("stored: %+v\n", structs.LocalUsers[decode.Username].DcList)
            fmt.Printf("struct: %+v\n", structs.LocalUsers[decode.Username].DcList)
        }
        fmt.Printf("in get stored: %+v\n", structs.LocalUsers)
    }
    cur.Close(context.TODO())
}

有问题的结构

//LocalUsers is just a slice of all users gathered from DB
var LocalUsers = make(map[string]User)

// User describes a user
type User struct {
    Username string
    Password string
    FullName string
    Email    string
    Role     string
    DcList   []string
}

这个概念在我的代码的其他部分中起作用,但是在这里看来,从将对象插入到mongo中的测试来看,一切都按预期工作。我得到了我期望的结果,等等。 当我实际上重新启动应用程序时,我注意到除DcList片以外的所有信息都与我期望的一样。它总是被循环中的最后一项覆盖。我做了一些测试,但我无法确定为什么会发生这种情况?

stored: {Username:Admin Password:peopleeater FullName:Administrator Email:admin@here.com Role:Admin DcList:[]}
struct: {Username:Admin Password:peopleeater FullName:Administrator Email:admin@here.com Role:Admin DcList:[]}
stored: {Username:bob Password:password FullName:bob Email:bob@here.com Role:View DcList:[eriks]}
struct: {Username:bob Password:password FullName:bob Email:bob@here.com Role:View DcList:[eriks]}
stored: {Username:test Password:test1 FullName:test1 Email:test1@here.com Role:View DcList:[test1]}
struct: {Username:test Password:test1 FullName:test1 Email:test1@here.com Role:View DcList:[test1]}
stored: {Username:test2 Password:test2 FullName:test2 Email:test2@here.com Role:View DcList:[test2]}
struct: {Username:test2 Password:test2 FullName:test2 Email:test2@here.com Role:View DcList:[test2]}
stored: {Username:test3 Password:test3 FullName:test3 Email:test3@here.com Role:View DcList:[test1]}
struct: {Username:test3 Password:test3 FullName:test3 Email:test3@here.com Role:View DcList:[test1]}
in get stored: map[Admin:{Username:Admin Password:P@$$w0rd FullName:Administrator Email:admin@here.com Role:Admin DcList:[]} bob:{Username:bob Password:password FullName:bob Email:bob@here.com Role:View DcList:[test1]} test:{Username:test Password:test1 FullName:test1 Email:test1@here.com Role:View DcList:[test1]} test2:{Username:test2 Password:test2 FullName:test2 Email:test2@here.com Role:View DcList:[test1]} test3:{Username:test3 Password:test3 FullName:test3 Email:test3@here.com Role:View DcList:[test1]}]

我考虑过也许应该初始化切片?但是我似乎过去并不需要这样做,所以我很困惑现阶段在哪里发生?我知道我想念的东西很简单

0 个答案:

没有答案