[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-worker-pool-go-all--*":3,"academy-blog-translations-io7pwb1vn59n5fp":125},{"data":4,"page":112,"perPage":112,"totalItems":112,"totalPages":112},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":120,"keywords":121,"locale":94,"published_at":122,"scheduled_at":13,"school_blog":116,"short_description":123,"status":114,"title":6,"updated":124,"updated_by":13,"slug":117,"views":119},"Ep.19 Go and Worker Pool: Enhancing Concurrent Processing!","sclblg987654321","school_blog_translations","\u003Cp class=\"p1\">\u003Cstrong>Go and Worker Pool: Enhancing Concurrent Processing!\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>What is a Worker Pool?\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">A Worker Pool is a structure that divides tasks into multiple parts and distributes them to \"Workers\" that operate concurrently. This allows the program to process tasks more quickly without waiting for one Worker to finish before starting the next task.\u003C\u002Fp>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Creating a Worker Pool in Go\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">We create a Worker Pool using Goroutines and Channels to communicate between tasks and Workers.\u003C\u002Fp>\u003Cp class=\"p4\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Steps to Create a Simple Worker Pool:\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">1. Create Channels for Tasks and Results\u003C\u002Fp>\u003Cp class=\"p3\">We will create a Channel to send tasks to Workers and another Channel to receive results sent back from Workers.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">jobs := make(chan int, 5)     \u002F\u002F ช่องรับงาน\r\nresults := make(chan int, 5)  \u002F\u002F ช่องรับผลลัพธ์\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">2. Create a Worker Function\u003C\u002Fp>\u003Cp class=\"p3\">The Worker will perform tasks received from the Channel and send results back through the results Channel.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">func worker(id int, jobs &lt;-chan int, results chan&lt;- int) {\r\n    for job := range jobs {\r\n        fmt.Printf(\"Worker %d started job %d\\n\", id, job)\r\n        time.Sleep(time.Second) \u002F\u002F จำลองเวลาทำงาน\r\n        fmt.Printf(\"Worker %d finished job %d\\n\", id, job)\r\n        results &lt;- job * 2 \u002F\u002F ส่งผลลัพธ์กลับไปยัง results\r\n    }\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">3. Start Multiple Workers Concurrently\u003C\u002Fp>\u003Cp class=\"p3\">We can create multiple Workers (Goroutines) to receive tasks from the Channel.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">for w := 1; w &lt;= 3; w++ { \u002F\u002F สร้าง Worker 3 ตัว\r\n    go worker(w, jobs, results)\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">4. Send Tasks into the Task Channel\u003C\u002Fp>\u003Cp class=\"p3\">We will send tasks into the jobs Channel for Workers to process.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">for j := 1; j &lt;= 5; j++ {\r\n    jobs &lt;- j\r\n}\r\nclose(jobs) \u002F\u002F ปิด Channel ของงานเมื่อส่งงานครบ\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">5. Receive Results from the Results Channel\u003C\u002Fp>\u003Cp class=\"p3\">Receive results from the results Channel, which the Workers have sent back.\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">for a := 1; a &lt;= 5; a++ {\r\n    result := &lt;-results\r\n    fmt.Println(\"Result:\", result)\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp class=\"p3\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Full Example Code of a Worker Pool\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">The program creates 3 Workers and sends 5 tasks for them to handle. Each Worker performs its assigned tasks concurrently and sends the results back to the results Channel.\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 worker(id int, jobs &lt;-chan int, results chan&lt;- int) {\r\n    for job := range jobs {\r\n        fmt.Printf(\"Worker %d started job %d\\n\", id, job)\r\n        time.Sleep(time.Second) \u002F\u002F จำลองเวลาทำงาน\r\n        fmt.Printf(\"Worker %d finished job %d\\n\", id, job)\r\n        results &lt;- job * 2 \u002F\u002F ส่งผลลัพธ์\r\n    }\r\n}\r\n\r\nfunc main() {\r\n    jobs := make(chan int, 5)\r\n    results := make(chan int, 5)\r\n\r\n    for w := 1; w &lt;= 3; w++ {\r\n        go worker(w, jobs, results)\r\n    }\r\n\r\n    for j := 1; j &lt;= 5; j++ {\r\n        jobs &lt;- j\r\n    }\r\n    close(jobs)\r\n\r\n    for a := 1; a &lt;= 5; a++ {\r\n        fmt.Println(\"Result:\", &lt;-results)\r\n    }\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Benefits of a Worker Pool:\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul class=\"ul1\">\u003Cli class=\"li3\">\u003Cstrong>Resource Efficiency:\u003C\u002Fstrong> Perform multiple tasks concurrently using a limited number of Workers.\u003C\u002Fli>\u003Cli class=\"li3\">\u003Cstrong>Improved Performance:\u003C\u002Fstrong> Reduced wait times as tasks are divided into smaller parts.\u003C\u002Fli>\u003Cli class=\"li3\">\u003Cstrong>Easier Management:\u003C\u002Fstrong> Suitable for repetitive tasks in large volumes.\u003C\u002Fli>\u003C\u002Ful>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>In Summary:\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul class=\"ul1\">\u003Cli class=\"li3\">Create Channels for sending tasks and receiving results.\u003C\u002Fli>\u003Cli class=\"li3\">Use Goroutines to create multiple Workers.\u003C\u002Fli>\u003Cli class=\"li3\">Send tasks to Workers through Channels and receive results when completed.\u003C\u002Fli>\u003C\u002Ful>","epagst2foy0s_t04ne5tnhr.16.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fls3imq1gtomwdif\u002Fepagst2foy0s_t04ne5tnhr.16.webp","2026-03-04 08:34:36.435Z","",{"keywords":15,"locale":88,"school_blog":98},[16,23,28,33,38,43,48,53,58,63,68,73,78,83],{"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:31:54.955Z","264sfjffyhspetq","programmers","2026-04-10 16:07:47.221Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"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":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:32:15.843Z","m0x7wo77i8iycf1","Programming Education","2026-04-10 16:07:51.675Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"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":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:34:31.488Z","ogf8aolgxlwt0ys","Task Management","2026-04-10 16:08:16.463Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:34:32.632Z","5j85phya54e35es","Efficiency","2026-04-10 16:08:17.110Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:33:55.714Z","yxs12a8pjduka2m","Channels","2026-04-10 16:08:03.494Z",{"collectionId":17,"collectionName":18,"created":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:33:58.044Z","nb6p1r8sfqlsxf8","Goroutines","2026-04-10 16:08:04.493Z",{"collectionId":17,"collectionName":18,"created":64,"created_by":13,"id":65,"name":66,"updated":67,"updated_by":13},"2026-03-04 08:34:33.281Z","9c0xd3phnszr39n","Concurrent Processing","2026-04-10 16:08:17.260Z",{"collectionId":17,"collectionName":18,"created":69,"created_by":13,"id":70,"name":71,"updated":72,"updated_by":13},"2026-03-04 08:34:34.471Z","wdd8fb47sbrmq28","Worker Pool","2026-04-10 16:08:17.741Z",{"collectionId":17,"collectionName":18,"created":74,"created_by":13,"id":75,"name":76,"updated":77,"updated_by":13},"2026-03-04 08:33:59.315Z","btmgtfwmgpke1aa","Go language","2026-04-10 16:08:04.625Z",{"collectionId":17,"collectionName":18,"created":79,"created_by":13,"id":80,"name":81,"updated":82,"updated_by":13},"2026-03-04 08:20:14.253Z","ah6lvy4x8qe08l5","Golang","2026-04-10 16:07:26.172Z",{"collectionId":17,"collectionName":18,"created":84,"created_by":13,"id":85,"name":86,"updated":87,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"code":89,"collectionId":90,"collectionName":91,"created":92,"flag":93,"id":94,"is_default":95,"label":96,"updated":97},"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":99,"collectionId":100,"collectionName":101,"created":13,"expand":102,"id":116,"slug":117,"updated":118,"views":119},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":103},{"blogIds":104,"collectionId":105,"collectionName":106,"created":107,"created_by":13,"id":99,"image":108,"image_alt":13,"image_path":109,"label":110,"name":111,"priority":112,"publish_at":113,"scheduled_at":13,"status":114,"updated":115,"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":111,"th":111},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","io7pwb1vn59n5fp","worker-pool-go","2026-05-11 17:32:45.802Z",403,"ls3imq1gtomwdif",[20,25,30,35,40,45,50,55,60,65,70,75,80,85],"2025-01-27 04:37:24.895Z","Learn how to create a Worker Pool in Go to enhance concurrent processing efficiency and manage multiple tasks effectively.","2026-05-06 08:37:52.931Z",{"th":117,"en":117}]