Open
Description
This tutorial https://go.dev/doc/tutorial/web-service-gin uses package variable album
to read and write from http handlers.
However there is no synchronization applied to avoid race condition.
// postAlbums adds an album from JSON received in the request body.
func postAlbums(c *gin.Context) {
var newAlbum album
// Call BindJSON to bind the received JSON to
// newAlbum.
if err := c.BindJSON(&newAlbum); err != nil {
return
}
// Add the new album to the slice.
albums = append(albums, newAlbum) // <-- DATA RACE.
c.IndentedJSON(http.StatusCreated, newAlbum)
}
If this is intentional to keep the tutorial simple I think it makes sense to warn the reader about code limitations, because high level of concurrency is natural for http servers.
Or maybe it would be even better to avoid race with a different design.
Thanks.