{"id":50,"date":"2019-03-17T04:46:47","date_gmt":"2019-03-17T04:46:47","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=50"},"modified":"2019-11-13T16:11:15","modified_gmt":"2019-11-13T16:11:15","slug":"go-maps-concurrency","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/go-maps-concurrency\/","title":{"rendered":"Golang Maps: Not safe for concurrent use"},"content":{"rendered":"\n

If you are a golang web application developer, then you often might encounter a use case of storing some data in memory in a go map for frequent access. If that is the case with you then you need to be careful because go maps are not safe for concurrent use. <\/p>\n\n\n\n

Buggy code:<\/strong> Below is buggy code. It might result in a crash if concurrent read and write of map happen.<\/p>\n\n\n\n

package main\n\nvar (\n   allData = make(map[string]string)\n)\n\nfunc get(key string) string {\n\treturn allData[key]\n}\n\nfunc set(key string, value string) {\n\tallData[key] = value\n}\n\nfunc main() {\n\tgo set(\"a\", \"Some Data 1\")\n\tgo set(\"b\", \"Some Data 2\")\n\tgo get(\"a\")\n\tgo get(\"b\")\n\tgo get(\"a\")\n}\n<\/code><\/pre>\n\n\n\n

Possible Output: fatal error: concurrent map read and map write<\/p>\n\n\n\n

Correct Code:<\/strong><\/p>\n\n\n\n

package main\n\nimport (\n\t\"fmt\"\n\t\"sync\"\n)\n\nvar (\n\tallData = make(map[string]string)\n\trwm     sync.RWMutex\n)\n\nfunc get(key string) string {\n\trwm.RLock()\n\tdefer rwm.RUnlock()\n\treturn allData[key]\n\n}\n\nfunc set(key string, value string) {\n\trwm.Lock()\n\tdefer rwm.Unlock()\n\tallData[key] = value\n\n}\n\nfunc main() {\n\tset(\"a\", \"Some Data\")\n\tresult := get(\"a\")\n\tfmt.Println(result)\n}\n<\/code><\/pre>\n\n\n\n

Output:<\/strong> Some Data<\/p>\n","protected":false},"excerpt":{"rendered":"

If you are a golang web application developer, then you often might encounter a use case of storing some data in memory in a go map for frequent access. If that is…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"class_list":["post-50","post","type-post","status-publish","format-standard","hentry","category-tech"],"yoast_head":"\nGolang Maps: Not safe for concurrent use - Welcome To Golang By Example<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/golangbyexamples.com\/go-maps-concurrency\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Golang Maps: Not safe for concurrent use - Welcome To Golang By Example\" \/>\n<meta property=\"og:description\" content=\"If you are a golang web application developer, then you often might encounter a use case of storing some data in memory in a go map for frequent access. If that is...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/golangbyexamples.com\/go-maps-concurrency\/\" \/>\n<meta property=\"og:site_name\" content=\"Welcome To Golang By Example\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-17T04:46:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-13T16:11:15+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/golangbyexamples.com\/go-maps-concurrency\/\",\"url\":\"https:\/\/golangbyexamples.com\/go-maps-concurrency\/\",\"name\":\"Golang Maps: Not safe for concurrent use - Welcome To Golang By Example\",\"isPartOf\":{\"@id\":\"https:\/\/golangbyexamples.com\/#website\"},\"datePublished\":\"2019-03-17T04:46:47+00:00\",\"dateModified\":\"2019-11-13T16:11:15+00:00\",\"author\":{\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\"},\"breadcrumb\":{\"@id\":\"https:\/\/golangbyexamples.com\/go-maps-concurrency\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/golangbyexamples.com\/go-maps-concurrency\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/golangbyexamples.com\/go-maps-concurrency\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/golangbyexamples.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Golang Maps: Not safe for concurrent use\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/golangbyexamples.com\/#website\",\"url\":\"https:\/\/golangbyexamples.com\/\",\"name\":\"Welcome To Golang By Example\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/golangbyexamples.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/golangbyexamples.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Golang Maps: Not safe for concurrent use - Welcome To Golang By Example","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/golangbyexamples.com\/go-maps-concurrency\/","og_locale":"en_US","og_type":"article","og_title":"Golang Maps: Not safe for concurrent use - Welcome To Golang By Example","og_description":"If you are a golang web application developer, then you often might encounter a use case of storing some data in memory in a go map for frequent access. If that is...","og_url":"https:\/\/golangbyexamples.com\/go-maps-concurrency\/","og_site_name":"Welcome To Golang By Example","article_published_time":"2019-03-17T04:46:47+00:00","article_modified_time":"2019-11-13T16:11:15+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/golangbyexamples.com\/go-maps-concurrency\/","url":"https:\/\/golangbyexamples.com\/go-maps-concurrency\/","name":"Golang Maps: Not safe for concurrent use - Welcome To Golang By Example","isPartOf":{"@id":"https:\/\/golangbyexamples.com\/#website"},"datePublished":"2019-03-17T04:46:47+00:00","dateModified":"2019-11-13T16:11:15+00:00","author":{"@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f"},"breadcrumb":{"@id":"https:\/\/golangbyexamples.com\/go-maps-concurrency\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/golangbyexamples.com\/go-maps-concurrency\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/golangbyexamples.com\/go-maps-concurrency\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/golangbyexamples.com\/"},{"@type":"ListItem","position":2,"name":"Golang Maps: Not safe for concurrent use"}]},{"@type":"WebSite","@id":"https:\/\/golangbyexamples.com\/#website","url":"https:\/\/golangbyexamples.com\/","name":"Welcome To Golang By Example","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/golangbyexamples.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/golangbyexamples.com\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paOs1b-O","amp_validity":null,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/50","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":11,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"predecessor-version":[{"id":532,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/50\/revisions\/532"}],"wp:attachment":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}