Describe how you will solve the problem of caching the car price: data structure, synchronization, update.
iOS
func getCarPrice() int64 { time.Sleep(1 * time.Second) return rand.Int63n(1000) } func main() { http.HandleFunc("/car/instant", func(resp http.ResponseWriter, req *http.Request) { fmt.Fprintf(resp, "%d", 0) }) http.ListenAndServe(":8080", nil) }
Why doesn't the current code with an infinite for loop in main work? How to fix it?
Why do some requests return a price that was never there (for example, an intermediate value between the old and new price)?
You need to write a simple in-memory cache library. type Cache interface { Set(k, v string) Get(k string) (v string, ok bool) }
Why does adding a mutex only for writing not solve the data race problem? How to correctly use a mutex to protect a shared variable?