[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-concurrency-go-goroutines-channels-all--*":3,"academy-blog-translations-jll3xmsbijyif36":119},{"data":4,"page":107,"perPage":107,"totalItems":107,"totalPages":107},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":113,"keywords":114,"locale":89,"published_at":115,"scheduled_at":13,"school_blog":111,"short_description":116,"slug":117,"status":109,"title":6,"updated":118,"updated_by":13,"views":112},"Ep.18 Go and Concurrency: Doing Multiple Things Smoothly!","sclblg987654321","school_blog_translations","\u003Cp class=\"p1\">\u003Cstrong>Go and Concurrency: Doing Multiple Things Smoothly!\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>What is Concurrency?\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Concurrency is the ability to perform multiple tasks simultaneously without waiting for each task to complete one after another. For example, loading data while waiting for a response from a server. In Go, we have Goroutines that make managing concurrency easier.\u003C\u002Fp>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Working with Goroutines\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Goroutines are functions in Go that operate in the background. When we call a function with the keyword go, that function starts running as a Goroutine immediately without blocking the main program.\u003Cbr>Example of using Goroutine:\u003Cbr>In this example, the printNumbers function will run as a Goroutine, operating in the background without blocking main().\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"time\"\r\n)\r\n\r\nfunc printNumbers() {\r\n    for i := 1; i &lt;= 5; i++ {\r\n        fmt.Println(i)\r\n        time.Sleep(500 * time.Millisecond)\r\n    }\r\n}\r\n\r\nfunc main() {\r\n    go printNumbers() \u002F\u002F เริ่ม Goroutine\r\n    fmt.Println(\"Goroutine started\")\r\n    time.Sleep(3 * time.Second) \u002F\u002F รอให้ Goroutine ทำงานเสร็จ\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Communicating Between Goroutines Using Channels\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Channels are tools for sending data between Goroutines, allowing us to control the operations of Goroutines and send data back and forth.\u003Cbr>Example of using Channel:\u003Cbr>In this example:\u003Cbr>\u003Ci>ch := make(chan string) creates a Channel for sending string data.\u003C\u002Fi>\u003Cbr>\u003Ci>ch &lt;- \"Hello from Goroutine\" sends a message from the Goroutine to the Channel.\u003C\u002Fi>\u003Cbr>\u003Ci>msg := &lt;-ch receives the message from the Channel and prints it out.\u003C\u002Fi>\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n)\r\n\r\nfunc printMessage(ch chan string) {\r\n    ch &lt;- \"Hello from Goroutine\" \u002F\u002F ส่งข้อความผ่าน Channel\r\n}\r\n\r\nfunc main() {\r\n    ch := make(chan string)\r\n\r\n    go printMessage(ch)\r\n\r\n    msg := &lt;-ch \u002F\u002F รับข้อความจาก Channel\r\n    fmt.Println(msg)\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Buffered Channels - Storing Multiple Values Before Use\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Buffered Channels allow us to send multiple values into a Channel before using them, improving efficiency and reducing data queuing.\u003Cbr>Example of using Buffered Channel:\u003Cbr>In this example:\u003Cbr>The Channel ch is created to hold 3 values.\u003Cbr>All data will be sent into the Channel and retrieved one value at a time without queuing.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n)\r\n\r\nfunc main() {\r\n    ch := make(chan int, 3) \u002F\u002F Channel ที่เก็บข้อมูลได้ 3 ค่า\r\n\r\n    ch &lt;- 1\r\n    ch &lt;- 2\r\n    ch &lt;- 3\r\n\r\n    fmt.Println(&lt;-ch) \u002F\u002F แสดงค่า 1\r\n    fmt.Println(&lt;-ch) \u002F\u002F แสดงค่า 2\r\n    fmt.Println(&lt;-ch) \u002F\u002F แสดงค่า 3\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Using Select to Manage Multiple Channels\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">When we have multiple Channels, select helps us manage communication between Channels more easily.\u003Cbr>Example of using select with Channel:\u003Cbr>In this example:\u003Cbr>The program will wait until data from any one of the Channels is available.\u003Cbr>select allows us to manage data transmission between multiple Channels efficiently.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"time\"\r\n)\r\n\r\nfunc main() {\r\n    ch1 := make(chan string)\r\n    ch2 := make(chan string)\r\n\r\n    go func() {\r\n        time.Sleep(2 * time.Second)\r\n        ch1 &lt;- \"Message from channel 1\"\r\n    }()\r\n\r\n    go func() {\r\n        time.Sleep(1 * time.Second)\r\n        ch2 &lt;- \"Message from channel 2\"\r\n    }()\r\n\r\n    select {\r\n    case msg1 := &lt;-ch1:\r\n        fmt.Println(msg1)\r\n    case msg2 := &lt;-ch2:\r\n        fmt.Println(msg2)\r\n    }\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>In Summary\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul class=\"ul1\">\u003Cli class=\"li3\">Use go to start a Goroutine running in the background.\u003C\u002Fli>\u003Cli class=\"li3\">Use Channels to send data between Goroutines.\u003C\u002Fli>\u003Cli class=\"li3\">Use Buffered Channels to store multiple values before use.\u003C\u002Fli>\u003Cli class=\"li3\">Use select to manage operations between multiple Channels.\u003C\u002Fli>\u003C\u002Ful>\u003Cp class=\"p3\">Try creating a program that uses Goroutines and Channels to simulate receiving messages from multiple users, while storing the received messages in a Channel and displaying the results one by one in order!\u003C\u002Fp>","6_11zon_rep0cinori.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002F98xzw93z6wjljer\u002F6_11zon_rep0cinori.webp","2026-03-04 08:34:35.101Z","",{"keywords":15,"locale":83,"school_blog":93},[16,23,28,33,38,43,48,53,58,63,68,73,78],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:26:59.195Z","gab60xd583s3qaw","Superdev School","2026-04-10 16:07:37.087Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:31:54.955Z","264sfjffyhspetq","programmers","2026-04-10 16:07:47.221Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:31:49.362Z","2m9vv13etpn6zkx","programming language","2026-04-10 16:07:45.606Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:34:32.401Z","9jwj4jb7uujr73c","programming technology","2026-04-10 16:08:16.944Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:32:26.073Z","vnvj1oaxje9m1q8","programming for beginners","2026-04-10 16:07:54.133Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:32:09.324Z","gon9gv2r39iu34p","programming development","2026-04-10 16:07:50.515Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:32:15.843Z","m0x7wo77i8iycf1","Programming Education","2026-04-10 16:07:51.675Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:31:22.575Z","lfjse4xivbgg5wu","Practice programming","2026-04-10 16:07:39.541Z",{"collectionId":17,"collectionName":18,"created":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:33:55.714Z","yxs12a8pjduka2m","Channels","2026-04-10 16:08:03.494Z",{"collectionId":17,"collectionName":18,"created":64,"created_by":13,"id":65,"name":66,"updated":67,"updated_by":13},"2026-03-04 08:33:58.044Z","nb6p1r8sfqlsxf8","Goroutines","2026-04-10 16:08:04.493Z",{"collectionId":17,"collectionName":18,"created":69,"created_by":13,"id":70,"name":71,"updated":72,"updated_by":13},"2026-03-04 08:24:48.143Z","dourw0uuydrrh1h","Concurrency","2026-04-10 16:07:30.157Z",{"collectionId":17,"collectionName":18,"created":74,"created_by":13,"id":75,"name":76,"updated":77,"updated_by":13},"2026-03-04 08:20:14.253Z","ah6lvy4x8qe08l5","Golang","2026-04-10 16:07:26.172Z",{"collectionId":17,"collectionName":18,"created":79,"created_by":13,"id":80,"name":81,"updated":82,"updated_by":13},"2026-03-04 08:33:59.315Z","btmgtfwmgpke1aa","Go language","2026-04-10 16:08:04.625Z",{"code":84,"collectionId":85,"collectionName":86,"created":87,"flag":88,"id":89,"is_default":90,"label":91,"updated":92},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":94,"collectionId":95,"collectionName":96,"expand":97,"id":111,"views":112},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":98},{"blogIds":99,"collectionId":100,"collectionName":101,"created":102,"created_by":13,"id":94,"image":103,"image_alt":13,"image_path":104,"label":105,"name":106,"priority":107,"publish_at":108,"scheduled_at":13,"status":109,"updated":110,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw.png",{"en":106,"th":106},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","jll3xmsbijyif36",210,"98xzw93z6wjljer",[20,25,30,35,40,45,50,55,60,65,70,75,80],"2025-01-27 04:37:14.738Z","Learn about concurrency in Go with Goroutines and Channels to enhance programming efficiency.","concurrency-go-goroutines-channels","2026-04-25 02:47:33.411Z",{"en":117}]