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:
[
{
"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:
{
"Email": "email",
"Password": "password"
}
I'm new to go and have no idea on how to do it.