[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-concurrency-go-goroutines-channels-all--*":3,"academy-blog-translations-n34rll7za4kqa4s":129},{"data":4,"page":117,"perPage":117,"totalItems":117,"totalPages":117},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":123,"keywords":124,"locale":99,"published_at":125,"scheduled_at":13,"school_blog":121,"short_description":126,"slug":127,"status":119,"title":6,"updated":128,"updated_by":13,"views":122},"Ep.18 Go กับ Concurrency ทำหลายอย่างพร้อมกันอย่างลื่นไหล!","sclblg987654321","school_blog_translations","\u003Cp class=\"p1\">\u003Cstrong>Go กับ Concurrency ทำหลายอย่างพร้อมกันอย่างลื่นไหล!\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Concurrency คืออะไร?\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Concurrency คือความสามารถในการทำงานหลายอย่างพร้อมกัน โดยไม่ต้องรอให้แต่ละงานทำเสร็จทีละงาน เช่น การโหลดข้อมูลขณะรอการตอบสนองจากเซิร์ฟเวอร์ ใน Go เรามี Goroutines ที่ทำให้จัดการ Concurrency ได้ง่าย\u003C\u002Fp>\u003Cp class=\"p2\">&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>การทำงานกับ Goroutines\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Goroutines เป็นฟังก์ชันใน Go ที่ทำงานแบบเบื้องหลัง เมื่อเราเรียกฟังก์ชันโดยใช้ go ฟังก์ชันนั้นจะเริ่มทำงานเป็น Goroutine ทันที โดยไม่บล็อกโปรแกรมหลัก\u003C\u002Fp>\u003Cp class=\"p3\">ตัวอย่างการใช้งาน Goroutine:\u003C\u002Fp>\u003Cp class=\"p3\">ในตัวอย่างนี้: ฟังก์ชัน printNumbers จะทำงานเป็น Goroutine ซึ่งทำงานแบบเบื้องหลังโดยไม่บล็อก 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>การสื่อสารระหว่าง Goroutines ด้วย Channels\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Channels คือเครื่องมือสำหรับส่งข้อมูลระหว่าง Goroutines ทำให้เราสามารถควบคุมการทำงานของ Goroutines และส่งข้อมูลระหว่างกันได้\u003C\u002Fp>\u003Cp class=\"p3\">ตัวอย่างการใช้งาน Channel:\u003C\u002Fp>\u003Cp class=\"p3\">ในตัวอย่างนี้:\u003C\u002Fp>\u003Cp class=\"p3\">\u003Ci>ch := make(chan string) \u003C\u002Fi>สร้าง\u003Ci> Channel \u003C\u002Fi>สำหรับส่งข้อมูลแบบข้อความ\u003C\u002Fp>\u003Cp class=\"p3\">\u003Ci>ch &lt;- \"Hello from Goroutine\" \u003C\u002Fi>ส่งข้อความจาก\u003Ci> Goroutine \u003C\u002Fi>ไปยัง\u003Ci> Channel\u003C\u002Fi>\u003C\u002Fp>\u003Cp class=\"p3\">\u003Ci>msg := &lt;-ch \u003C\u002Fi>รับข้อความจาก\u003Ci> Channel \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>&nbsp;\u003C\u002Fp>\u003Cp class=\"p3\">\u003Cstrong>Buffered Channels - จัดเก็บข้อมูลหลายค่าก่อนนำไปใช้งาน\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">Buffered Channels ช่วยให้เราส่งข้อมูลได้หลายค่าใน Channel ก่อนนำไปใช้งาน ช่วยเพิ่มประสิทธิภาพและลดการรอคิวข้อมูล\u003C\u002Fp>\u003Cp class=\"p3\">ตัวอย่างการใช้งาน Buffered Channel:\u003C\u002Fp>\u003Cp class=\"p3\">ในตัวอย่างนี้:\u003C\u002Fp>\u003Cp class=\"p3\">Channel ch ถูกสร้างให้เก็บได้ 3 ค่า\u003C\u002Fp>\u003Cp class=\"p3\">ข้อมูลทั้งหมดจะถูกส่งเข้า Channel แล้วดึงออกมาทีละค่าโดยไม่มีการรอคิว\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>การใช้ select สำหรับจัดการหลาย Channels\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp class=\"p3\">ในกรณีที่เรามีหลาย Channel select ช่วยให้เราสามารถจัดการการสื่อสารระหว่าง Channel ได้ง่ายขึ้น\u003C\u002Fp>\u003Cp class=\"p3\">ตัวอย่างการใช้ select กับ Channel:\u003C\u002Fp>\u003Cp class=\"p3\">ในตัวอย่างนี้:\u003C\u002Fp>\u003Cp class=\"p3\">โปรแกรมจะรอจนกว่าจะมีข้อมูลจาก Channel ใด Channel หนึ่งพร้อมใช้งาน\u003C\u002Fp>\u003Cp class=\"p3\">select ช่วยให้เราสามารถจัดการการรับส่งข้อมูลระหว่าง 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 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>สรุปง่ายๆ\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul class=\"ul1\">\u003Cli class=\"li3\">ใช้ go เพื่อเริ่ม Goroutine ทำงานเบื้องหลัง\u003C\u002Fli>\u003Cli class=\"li3\">ใช้ Channel เพื่อส่งข้อมูลระหว่าง Goroutines\u003C\u002Fli>\u003Cli class=\"li3\">ใช้ Buffered Channels สำหรับเก็บข้อมูลหลายค่าก่อนใช้งาน\u003C\u002Fli>\u003Cli class=\"li3\">ใช้ select เพื่อจัดการการทำงานระหว่างหลาย Channels\u003C\u002Fli>\u003C\u002Ful>\u003Cp class=\"p4\">ลองสร้างโปรแกรมที่ใช้\u003Cspan class=\"s2\"> Goroutines \u003C\u002Fspan>และ\u003Cspan class=\"s2\"> Channel \u003C\u002Fspan>เพื่อจำลองการรับข้อความจากผู้ใช้หลายคน\u003Cspan class=\"s2\"> \u003C\u002Fspan>พร้อมเก็บข้อความที่ได้รับใน\u003Cspan class=\"s2\"> Channel \u003C\u002Fspan>และแสดงผลลัพธ์ทีละข้อความตามลำดับ\u003Cspan class=\"s2\">!\u003C\u002Fspan>\u003C\u002Fp>","5_11zon_y2pvfuuqcr.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fherx5l0rr48rj7d\u002F5_11zon_y2pvfuuqcr.webp","2026-03-04 08:34:38.436Z","",{"keywords":15,"locale":93,"school_blog":103},[16,23,28,33,38,43,48,53,58,63,68,73,78,83,88],{"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:26:56.612Z","yf74wkqyamfc5qx","โปรแกรมเมอร์","2026-04-10 16:07:36.426Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:20:37.794Z","x2c5z8w1cr41ij3","ภาษาโปรแกรมมิ่ง","2026-04-10 16:07:27.917Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:31:53.015Z","801w5t09mfaa9hl","ภาษาโปรแกรม","2026-04-10 16:07:46.625Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:32:27.254Z","xl8ixz1jmfnab5f","ฝึกเขียนโปรแกรม","2026-04-10 16:07:54.377Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:32:29.161Z","uewvwdt9cf41o8n","การเขียนโปรแกรมสำหรับมือใหม่","2026-04-10 16:07:54.935Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:31:30.863Z","oyltq82epf0vqka","การเขียนโปรแกรม","2026-04-10 16:07:41.883Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:32:27.645Z","vfe4f15x4b3afpm","การศึกษาการเขียนโปรแกรม","2026-04-10 16:07:54.538Z",{"collectionId":17,"collectionName":18,"created":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:34:36.844Z","4smjjmcniovk2wa","เทคโนโลยีการเขียนโปรแกรม","2026-04-10 16:08:18.440Z",{"collectionId":17,"collectionName":18,"created":64,"created_by":13,"id":65,"name":66,"updated":67,"updated_by":13},"2026-03-04 08:27:15.893Z","keubtbdqa4mblx3","การพัฒนาโปรแกรม","2026-04-10 16:07:38.769Z",{"collectionId":17,"collectionName":18,"created":69,"created_by":13,"id":70,"name":71,"updated":72,"updated_by":13},"2026-03-04 08:33:55.714Z","yxs12a8pjduka2m","Channels","2026-04-10 16:08:03.494Z",{"collectionId":17,"collectionName":18,"created":74,"created_by":13,"id":75,"name":76,"updated":77,"updated_by":13},"2026-03-04 08:33:58.044Z","nb6p1r8sfqlsxf8","Goroutines","2026-04-10 16:08:04.493Z",{"collectionId":17,"collectionName":18,"created":79,"created_by":13,"id":80,"name":81,"updated":82,"updated_by":13},"2026-03-04 08:24:48.143Z","dourw0uuydrrh1h","Concurrency","2026-04-10 16:07:30.157Z",{"collectionId":17,"collectionName":18,"created":84,"created_by":13,"id":85,"name":86,"updated":87,"updated_by":13},"2026-03-04 08:33:59.518Z","7pqn3y8ffwzzde5","ภาษา Go","2026-04-10 16:08:04.777Z",{"collectionId":17,"collectionName":18,"created":89,"created_by":13,"id":90,"name":91,"updated":92,"updated_by":13},"2026-03-04 08:20:14.253Z","ah6lvy4x8qe08l5","Golang","2026-04-10 16:07:26.172Z",{"code":94,"collectionId":95,"collectionName":96,"created":97,"flag":98,"id":99,"is_default":100,"label":101,"updated":102},"th","pbc_1989393366","locales","2026-01-22 10:59:55.832Z","twemoji:flag-thailand","s8wri3bt4vgg2ji",true,"Thai","2026-04-10 15:42:46.614Z",{"category":104,"collectionId":105,"collectionName":106,"expand":107,"id":121,"views":122},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":108},{"blogIds":109,"collectionId":110,"collectionName":111,"created":112,"created_by":13,"id":104,"image":113,"image_alt":13,"image_path":114,"label":115,"name":116,"priority":117,"publish_at":118,"scheduled_at":13,"status":119,"updated":120,"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":116,"th":116},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","n34rll7za4kqa4s",289,"herx5l0rr48rj7d",[20,25,30,35,40,45,50,55,60,65,70,75,80,85,90],"2025-01-27 04:37:11.056Z","เรียนรู้เกี่ยวกับการทำงานแบบ Concurrency ใน Go ด้วยการใช้ Goroutines และ Channels เพื่อเพิ่มประสิทธิภาพในการพัฒนาโปรแกรม","concurrency-go-goroutines-channels","2026-04-25 02:47:34.381Z",{"th":127}]