[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-go-goroutines-concurrency-all--*":3,"academy-blog-translations-dg2baz26hth86wv":84},{"data":4,"page":72,"perPage":72,"totalItems":72,"totalPages":72},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":78,"keywords":79,"locale":54,"published_at":80,"scheduled_at":13,"school_blog":76,"short_description":81,"slug":82,"status":74,"title":6,"updated":83,"updated_by":13,"views":77},"EP.8 Go กับ Goroutines - ทำหลายงานพร้อมกันแบบมือโปร!","sclblg987654321","school_blog_translations","\u003Cp>\u003Cspan style=\"font-size:20px;\">\u003Cstrong>Go กับ Goroutines - ทำหลายงานพร้อมกันแบบมือโปร!\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>อยากให้โปรแกรมทำงานหลายอย่างพร้อมกันได้อย่างรวดเร็วไหม? วันนี้เราจะมาสอนการใช้ Goroutines และ Channels ใน Go ที่จะทำให้โปรแกรมของคุณประมวลผลได้ลื่นไหลและไม่ติดขัด\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>Goroutines คืออะไร?\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>Goroutine คือหน่วยประมวลผลขนาดเล็กที่ Go ใช้สำหรับการทำงานพร้อมกัน (concurrent). เมื่อเราสร้าง Goroutine, Go จะทำงานในเบื้องหลังโดยไม่บล็อกโปรแกรมหลัก ทำให้เราสามารถรันงานหลายงานไปพร้อมกันได้\u003Cbr>ตัวอย่าง Goroutine:\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>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>Channels: การสื่อสารระหว่าง Goroutines\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>Channels ใช้สำหรับส่งข้อมูลระหว่าง Goroutines ทำให้การทำงานพร้อมกันปลอดภัยและเป็นระเบียบมากขึ้น\u003Cbr>ตัวอย่าง Channel:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">func main() {\r\n    messages := make(chan string)\r\n\r\n    go func() {\r\n        messages &lt;- \"Hello from Goroutine\" \u002F\u002F ส่งข้อมูลผ่าน Channel\r\n    }()\r\n\r\n    msg := &lt;-messages \u002F\u002F รับข้อมูลจาก Channel\r\n    fmt.Println(msg)\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>Buffered Channels:\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>Channels สามารถบัฟเฟอร์เพื่อเก็บข้อมูลได้หลายค่า:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">func main() {\r\n    messages := make(chan string, 2) \u002F\u002F บัฟเฟอร์ได้ 2 ค่า\r\n\r\n    messages &lt;- \"First\"\r\n    messages &lt;- \"Second\"\r\n\r\n    fmt.Println(&lt;-messages)\r\n    fmt.Println(&lt;-messages)\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>การควบคุม Concurrency ด้วย sync\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>Go มีแพ็กเกจ sync สำหรับควบคุม Concurrency เช่นการรอให้ Goroutines ทำงานเสร็จ:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">import (\r\n    \"fmt\"\r\n    \"sync\"\r\n)\r\n\r\nfunc worker(wg *sync.WaitGroup, id int) {\r\n    fmt.Printf(\"Worker %d starting\\n\", id)\r\n    wg.Done() \u002F\u002F ลดตัวนับลง 1\r\n}\r\n\r\nfunc main() {\r\n    var wg sync.WaitGroup\r\n\r\n    for i := 1; i &lt;= 3; i++ {\r\n        wg.Add(1)\r\n        go worker(&amp;wg, i)\r\n    }\r\n\r\n    wg.Wait() \u002F\u002F รอให้ทุก Goroutine ทำงานเสร็จ\r\n    fmt.Println(\"All workers done\")\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>","15_11zon_gpt8tjqocq.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002F90i16qnsz5x048j\u002F15_11zon_gpt8tjqocq.webp","2026-03-04 08:33:59.692Z","",{"keywords":15,"locale":48,"school_blog":58},[16,23,28,33,38,43],{"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:33:57.434Z","hz551yc9f7fozes","การพัฒนาแอปพลิเคชัน","2026-04-10 16:08:04.227Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:33:54.929Z","otkayeo8kz7u62p","sync","2026-04-10 16:08:03.333Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:33:55.714Z","yxs12a8pjduka2m","Channels","2026-04-10 16:08:03.494Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:24:48.143Z","dourw0uuydrrh1h","Concurrency","2026-04-10 16:07:30.157Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:33:58.044Z","nb6p1r8sfqlsxf8","Goroutines","2026-04-10 16:08:04.493Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"code":49,"collectionId":50,"collectionName":51,"created":52,"flag":53,"id":54,"is_default":55,"label":56,"updated":57},"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":59,"collectionId":60,"collectionName":61,"expand":62,"id":76,"views":77},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs",{"category":63},{"blogIds":64,"collectionId":65,"collectionName":66,"created":67,"created_by":13,"id":59,"image":68,"image_alt":13,"image_path":69,"label":70,"name":71,"priority":72,"publish_at":73,"scheduled_at":13,"status":74,"updated":75,"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":71,"th":71},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-04-25 02:32:15.470Z","dg2baz26hth86wv",498,"90i16qnsz5x048j",[20,25,30,35,40,45],"2025-01-22 05:02:19.597Z","เรียนรู้การสร้าง Goroutines และการใช้ Channels เพื่อบริหารจัดการหลายงานพร้อมกันในภาษา Go","go-goroutines-concurrency","2026-04-25 02:47:29.354Z",{"th":82}]