When to use GO sync.Mutex with net/http and gorilla/mux? : As far as I know, the "net/http" package uses goroutines for the handlers. Is it necessary that I lock even a map with sync.Mutex in order to prevent possible bugs in the "nextId" function cause the function could count an old state of the map? 

Here is my example code:

    package main
    
    import (
    	"net/http"
    	"github.com/gorilla/mux"
    	"io/ioutil"
    	"fmt"
    )
    
    var testData = map[int]string {
    	1: "foo",
    	2: "bar",
    }
    
    func main() {
    	r := mux.NewRouter()
    	r.HandleFunc("/data", getData).Methods("GET")
    	r.HandleFunc("/data", addData).Methods("POST")
    	http.ListenAndServe(":3000", r)
    }
    
    func getData(writer http.ResponseWriter, request *http.Request) {
    	for k, v := range testData {
    		fmt.Fprintf(writer, "Key: %d\tValue: %v\n", k, v)
    	}
    }
    
    func addData(writer http.ResponseWriter, request *http.Request) {
    	if data, err := ioutil.ReadAll(request.Body); err == nil {
    		if len(data) == 0 {
    			writer.WriteHeader(http.StatusBadRequest)
    			return
    		}
    
    		id := nextId()
    		testData[id] = string(data)
    		url := request.URL.String()
    		writer.Header().Set("Location", fmt.Sprintf("%s", url))
    		writer.WriteHeader(http.StatusCreated)
    
    	} else {
    		writer.WriteHeader(http.StatusBadRequest)
    	}
    }
    
    func nextId() int {
    	id := 1
    
    	for k, _ := range testData {
    		if k >= id {
    			id = k + 1;
    		}
    	}
    
    	return id
    }