Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>package main import ( "encoding/json" "fmt" "sync" ) type Window struct { Height int64 `json:"Height"` Width int64 `json:"Width"` } type Room struct { mu sync.Mutex Windows []Window `json:"Windows"` } func main() { js := []byte(`{"Windows":[{"Height":10,"Width":20},{"Height":10,"Width":20}]}`) fmt.Printf("Should have 2 windows: %v\n", string(js)) var room Room _ = json.Unmarshal(js, &amp;room) var wg sync.WaitGroup // Add meny windows to room for i := 0; i &lt; 10; i++ { wg.Add(1) go func() { defer wg.Done() addWindow(&amp;room) }() } wg.Wait() js, _ = json.Marshal(room) fmt.Printf("Sould have 12 windows: %v\n", string(js)) } func addWindow(r *Room) { window := Window{1, 1} fmt.Printf("Adding %v to %v\n", window, r.Windows) r.mu.Lock() defer r.mu.Unlock() r.Windows = append(r.Windows, window) } Should have 2 windows: {"Windows":[{"Height":10,"Width":20},{"Height":10,"Width":20}]} Adding {1 1} to [{10 20} {10 20}] Adding {1 1} to [{10 20} {10 20} {1 1}] Adding {1 1} to [{10 20} {10 20} {1 1} {1 1}] Adding {1 1} to [{10 20} {10 20} {1 1} {1 1} {1 1}] Adding {1 1} to [{10 20} {10 20} {1 1} {1 1} {1 1} {1 1}] Adding {1 1} to [{10 20} {10 20} {1 1} {1 1} {1 1} {1 1} {1 1}] Adding {1 1} to [{10 20} {10 20} {1 1} {1 1} {1 1} {1 1} {1 1} {1 1}] Adding {1 1} to [{10 20} {10 20} {1 1} {1 1} {1 1} {1 1} {1 1} {1 1} {1 1}] Adding {1 1} to [{10 20} {10 20} {1 1} {1 1} {1 1} {1 1} {1 1} {1 1} {1 1} {1 1}] Adding {1 1} to [{10 20} {10 20} {1 1} {1 1} {1 1} {1 1} {1 1} {1 1} {1 1} {1 1} {1 1}] Sould have 12 windows: {"Windows":[{"Height":10,"Width":20},{"Height":10,"Width":20},{"Height":1,"Width":1},{"Height":1,"Width":1},{"Height":1,"Width":1},{"Height":1,"Width":1},{"Height":1,"Width":1},{"Height":1,"Width":1},{"Height":1,"Width":1},{"Height":1,"Width":1},{"Height":1,"Width":1},{"Height":1,"Width":1}]} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload