简体   繁体   English

如何在数组中写入 map 并将其添加到 go 中的 json 文件中?

[英]How to write a map inside a array and adding that to a json file in go?

I want to make a sign-up server using go where the users are stored in a JSON file called users.json with this format:我想使用 go 制作注册服务器,其中用户存储在名为users.json的 JSON 文件中,格式如下:

[
    {
        "email": "email",
        "password": "password"
    },
    {
        "email": "email",
        "password": "password"
    },
    ...
]

Right now I'm using this code:现在我正在使用这段代码:

func signup(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()

    type User struct {
        Email    string
        Password string
    }

    user := User{}
    user.Email = r.Form["email"][0]
    user.Password = r.Form["password"][0]

    content, err := json.Marshal(user)
    if err != nil {
        fmt.Println(err)
    }
    err = ioutil.WriteFile("users.json", content, 0644)
    if err != nil {
        log.Fatal(err)
    }
}

The issue with it is that if a new user signed up it deletes the old data in the JSON file and it stores the informations in this format:它的问题是,如果新用户注册它会删除 JSON 文件中的旧数据,并以这种格式存储信息:

{
    "Email": "email",
    "Password": "password"
}

I'm new to go and have no idea on how to do it.我是 go 的新手,不知道该怎么做。

Doc says: WriteFile writes data to the named file, creating it if necessary. Doc 说:WriteFile 将数据写入指定文件,并在必要时创建它。 If the file does not exist, WriteFile creates it with permissions perm (before umask);如果文件不存在,WriteFile 使用权限 perm(在 umask 之前)创建它; otherwise WriteFile truncates it before writing, without changing permissions.否则 WriteFile 在写入之前将其截断,而不更改权限。

https://pkg.go.dev/os#WriteFile https://pkg.go.dev/os#WriteFile

You must append to the JSON file.您必须 append 到 JSON 文件。 Follow this thread to see how you can perform JSON file append.按照此线程查看如何执行 JSON 文件 append。 https://stackoverflow.com/a/44642209/9787555 https://stackoverflow.com/a/44642209/9787555

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:在 Firestore 的 map 中添加数据,但新数据保存为 map 中的数组而不是字段 - Error: Adding data in a map in Firestore but the the new data saves as an array inside the map instead of a field GO AWS SDKv2:解析 json 文件并将值添加为 AWS Secretsmanager 的标签 - GO AWS SDKv2: Parsing json file and adding values as tags for AWS Secretsmanager Flutter - 如何获取 Firestore 中数组的 Map 数据作为下拉列表 - Flutter - How to get Map data of an Array inside Firestore as Dropdownlist 如何在 Cloud Firestore 的数组中增加 map 中的值? - How to increment a value in a map inside an array in Cloud Firestore? 如何从 Dropdownlist 中的 firestore 接收数组的 Map 数据值 - How to receive Map data value of an array from firestore inside Dropdownlist 如何在 Python 中直接在 s3 中的文件中写入 JSON? - How can I write JSON in file in s3 directly in Python? 如何读取/更新存储在地图中的值,然后在 Cloud Firestore 的数组中? - How to read/update values stored inside a map which is then inside of an array in Cloud Firestore? 如何通过 API 网关将 JSON 数组发布到 AWS Lambda? json:无法将 object 解组为 []interface {} 类型的 Go 值 - How to post a JSON array to AWS Lambda through API Gateway? json: cannot unmarshal object into Go value of type []interface {} 在 Go-Lang 中,我如何获取 JSON 文件并引用 css - In Go-Lang how can i take JSON file and quote to css 如何直接从 Go 中的 GCP 服务帐户 JSON 密钥文件创建 kube.netes.Clientset? - How can I create a kubernetes.Clientset directly from a GCP service account JSON key file in Go?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM