{"id":604,"date":"2022-09-06T15:39:17","date_gmt":"2022-09-06T15:39:17","guid":{"rendered":"https:\/\/melfaeforest.xhere.eu.org\/?p=604"},"modified":"2022-09-06T15:39:17","modified_gmt":"2022-09-06T15:39:17","slug":"learning-go-language-day-6","status":"publish","type":"post","link":"https:\/\/melfaeforest.xhere.eu.org\/?p=604","title":{"rendered":"Learning Go Language &#8211; Day 6"},"content":{"rendered":"\n<p><strong>Goroutines<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Creating Goroutines:\n- Use the \"go\" keyword in front of function call\n- When using anonymous functions, pass data as local variables\n\nSynchronization:\n- Use sync.WaitGroup to wait for groups of goroutines to complete\n- Use sync.Mutex and sync.RWMutex to protect data access\n\nParallelism:\n- By default, Go will use CPU threads equal to available cores\n- Change with runtime.GOMAXPROCS\n- More threads can increase performance, but too many can slow it down\n\nBest Practices:\n- Don't create goroutines in libraries\n  - Let consumer control concurrency\n- When creating a goroutine, know how it will end\n  - Avoids subtle memory leaks\n- Check for race conditions at compile time<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-default\"\/>\n\n\n\n<p><strong>Channels<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Channel Basics:\n- Create a channel with the <strong>make<\/strong> command\n  - make(chan int)\n- Send message into channel\n  - ch &lt;= val\n- Receive message from channel\n  - val := &lt;-ch\n- Can have multiple senders and receivers\n\nRestricting Data Flow:\n- Channels can be case into send-only or receive-only versions\n  - Send-only: chan &lt;- int\n  - Receive-only: &lt;-chan int\n\nBuffered Channels:\n- Channels block sender side until receiver is available\n- Block receiver side until message is available\n- Can decouple sender and receiver with buffered channels\n  - make(chan int, 50)\n- Use buffered channels when send and receiver have assymmetric loading\n\nSelect Statements:\n- Allows goroutine to monitor several channels at once\n  - Blocks if all channels block\n  - If multiple channels receive value simultaneously, behavior is undefined<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Goroutines Creating Goroutines: &#8211; Use the &#8220;go&#8221; keyword in front of function call &#8211; When using anonymous functions, pass data as local variables Synchronization: &#8211; Use sync.WaitGroup to wait for groups of goroutines to complete &#8211; Use sync.Mutex and sync.RWMutex to protect data access Parallelism: &#8211; By default, Go will use CPU threads equal to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-604","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=604"}],"version-history":[{"count":3,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/604\/revisions"}],"predecessor-version":[{"id":628,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/604\/revisions\/628"}],"wp:attachment":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}