[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-th-1-1-all-js2go-ep21-file-io-management-javascript-go-all--*":3,"academy-blog-translations-zh79ttiv8fqsd7h":99},{"data":4,"page":98,"perPage":98,"totalItems":98,"totalPages":98},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":92,"keywords":93,"locale":69,"published_at":94,"scheduled_at":13,"school_blog":90,"short_description":95,"slug":96,"status":88,"title":6,"updated":97,"updated_by":13,"views":91},"JS2GO EP.21 การจัดการไฟล์และ I\u002FO ใน JavaScript และ Go","sclblg987654321","school_blog_translations","\u003Cp>การจัดการไฟล์และ Input\u002FOutput (I\u002FO) เป็นทักษะสำคัญสำหรับนักพัฒนา ไม่ว่าจะเป็นงานเว็บ แอปพลิเคชัน หรือระบบ backend การอ่านและเขียนไฟล์อย่างมีประสิทธิภาพช่วยให้ระบบทำงานได้เสถียร ปลอดภัย และรองรับการประมวลผลข้อมูลขนาดใหญ่ ในบทความนี้เราจะเปรียบเทียบ การจัดการไฟล์ใน JavaScript (Node.js) และ Go พร้อมตัวอย่างโค้ดและแนวทางปฏิบัติที่เหมาะสม เพื่อให้คุณสามารถจัดการไฟล์ได้อย่างมืออาชีพ\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>I\u002FO ใน JavaScript (Node.js)\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>JavaScript บน Node.js มี module \u003Ccode inline=\"\">fs\u003C\u002Fcode> สำหรับจัดการไฟล์ รองรับทั้ง synchronous และ asynchronous\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>การอ่านไฟล์แบบ synchronous\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">const fs = require('fs');\n\ntry {\n  const data = fs.readFileSync('example.txt', 'utf8');\n  console.log('File content:', data);\n} catch (err) {\n  console.error('Error reading file:', err);\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>การอ่านไฟล์แบบ asynchronous\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">const fs = require('fs');\n\nfs.readFile('example.txt', 'utf8', (err, data) =&gt; {\n  if (err) {\n    console.error('Error reading file:', err);\n    return;\n  }\n  console.log('File content:', data);\n});\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>การเขียนไฟล์\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-javascript\">fs.writeFile('output.txt', 'Hello, world!', 'utf8', (err) =&gt; {\n  if (err) {\n    console.error('Error writing file:', err);\n    return;\n  }\n  console.log('File has been written.');\n});\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>ข้อดีของ Node.js I\u002FO\u003C\u002Fh3>\u003Cul>\u003Cli>รองรับ asynchronous ทำให้ไม่บล็อก Event Loop\u003C\u002Fli>\u003Cli>มี ecosystem ขนาดใหญ่ เช่น \u003Ccode inline=\"\">fs-extra\u003C\u002Fcode> สำหรับฟังก์ชันเพิ่มเติม\u003C\u002Fli>\u003Cli>เขียนโค้ดง่ายและยืดหยุ่น\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>ข้อจำกัด\u003C\u002Fh3>\u003Cul>\u003Cli>การจัดการไฟล์ขนาดใหญ่ต้องระวัง memory usage\u003C\u002Fli>\u003Cli>Async code อาจซับซ้อนสำหรับผู้เริ่มต้น\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>I\u002FO ใน Go\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Go มี package \u003Ccode inline=\"\">os\u003C\u002Fcode> และ \u003Ccode inline=\"\">io\u002Fioutil\u003C\u002Fcode> \u002F \u003Ccode inline=\"\">io\u003C\u002Fcode> สำหรับจัดการไฟล์และ I\u002FO\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>การอ่านไฟล์แบบง่าย\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport (\n    \"fmt\"\n    \"io\u002Fioutil\"\n    \"log\"\n)\n\nfunc main() {\n    data, err := ioutil.ReadFile(\"example.txt\")\n    if err != nil {\n        log.Fatal(err)\n    }\n    fmt.Println(\"File content:\", string(data))\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>การเขียนไฟล์\u003C\u002Fh3>\u003Cpre>\u003Ccode class=\"language-plaintext language-go\">package main\n\nimport (\n    \"io\u002Fioutil\"\n    \"log\"\n)\n\nfunc main() {\n    content := []byte(\"Hello, Go!\")\n    err := ioutil.WriteFile(\"output.txt\", content, 0644)\n    if err != nil {\n        log.Fatal(err)\n    }\n    fmt.Println(\"File has been written.\")\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>ข้อดีของ Go I\u002FO\u003C\u002Fh3>\u003Cul>\u003Cli>Type-safe และ error handling ชัดเจน\u003C\u002Fli>\u003Cli>รองรับการจัดการไฟล์ขนาดใหญ่และ concurrent I\u002FO ได้ดี\u003C\u002Fli>\u003Cli>Built-in library ครบถ้วนสำหรับ filesystem\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>ข้อจำกัด\u003C\u002Fh3>\u003Cul>\u003Cli>Syntax อาจ verbose สำหรับงานง่าย ๆ\u003C\u002Fli>\u003Cli>ต้องจัดการ error ทุกครั้ง ไม่สามารถ ignore ได้\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>แนวทางปฏิบัติที่เหมาะสม\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Col>\u003Cli>จัดการ error ทุกครั้ง\u003Cul>\u003Cli>JavaScript: ใช้ \u003Ccode inline=\"\">try\u002Fcatch\u003C\u002Fcode> หรือ callback error\u003C\u002Fli>\u003Cli>Go: ตรวจสอบ \u003Ccode inline=\"\">err\u003C\u002Fcode> หลังทุก I\u002FO operation\u003C\u002Fli>\u003C\u002Ful>\u003C\u002Fli>\u003Cli>เลือก synchronous หรือ asynchronous ให้เหมาะสม\u003Cul>\u003Cli>Node.js: ใช้ async\u002Fawait หรือ callback หากไม่ต้องการบล็อก server\u003C\u002Fli>\u003Cli>Go: ใช้ goroutine หรือ channel สำหรับ concurrent I\u002FO\u003C\u002Fli>\u003C\u002Ful>\u003C\u002Fli>\u003Cli>ระวังไฟล์ขนาดใหญ่\u003Cul>\u003Cli>ใช้ streaming หรือ buffered read\u002Fwrite แทนการโหลดทั้งหมดเข้าหน่วยความจำ\u003C\u002Fli>\u003C\u002Ful>\u003C\u002Fli>\u003Cli>ตั้งค่า permissions อย่างเหมาะสม\u003Cul>\u003Cli>Go: 0644, 0755 สำหรับการเขียนไฟล์\u003C\u002Fli>\u003Cli>Node.js: ตั้งค่า mode ใน \u003Ccode inline=\"\">writeFile\u003C\u002Fcode>\u003C\u002Fli>\u003C\u002Ful>\u003C\u002Fli>\u003C\u002Fol>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>สรุปเปรียบเทียบ\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cfigure class=\"table\">\u003Ctable>\u003Cthead>\u003Ctr>\u003Cth>Feature\u003C\u002Fth>\u003Cth>JavaScript (Node.js)\u003C\u002Fth>\u003Cth>Go\u003C\u002Fth>\u003C\u002Ftr>\u003C\u002Fthead>\u003Ctbody>\u003Ctr>\u003Ctd>Library\u003C\u002Ftd>\u003Ctd>fs, fs-extra\u003C\u002Ftd>\u003Ctd>os, io\u002Fioutil, bufio\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Async support\u003C\u002Ftd>\u003Ctd>Promise, callback, async\u002Fawait\u003C\u002Ftd>\u003Ctd>Goroutines + channels\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Error handling\u003C\u002Ftd>\u003Ctd>try\u002Fcatch, callback error\u003C\u002Ftd>\u003Ctd>return error (type-safe)\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Large files\u003C\u002Ftd>\u003Ctd>Streaming recommended\u003C\u002Ftd>\u003Ctd>bufio, streaming\u003C\u002Ftd>\u003C\u002Ftr>\u003Ctr>\u003Ctd>Concurrency\u003C\u002Ftd>\u003Ctd>Non-blocking async\u003C\u002Ftd>\u003Ctd>Concurrent goroutines\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003C\u002Ffigure>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>คำแนะนำ:\u003C\u002Fp>\u003Cul>\u003Cli>สำหรับ web server \u002F event-driven I\u002FO ใช้ Node.js จะยืดหยุ่นและ async ง่าย\u003C\u002Fli>\u003Cli>สำหรับ backend service \u002F concurrent I\u002FO \u002F performance-critical ใช้ Go จะมั่นใจและเร็วกว่า\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>ตอนต่อไป\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>ใน EP.22 ของซีรีส์ JS2GO เราจะพาคุณไปเรียนรู้ การทำงานกับ JSON: JavaScript กับ Go ต่างกันอย่างไร? พร้อมตัวอย่างโค้ด JSON parsing, serialization และแนวทางปฏิบัติที่เหมาะสม เพื่อให้คุณสามารถอ่าน เขียน และจัดการ JSON ได้อย่างมีประสิทธิภาพ\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cstrong>อ่านบทความ Series อื่นๆ\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FGolang\">\u003Cstrong>Golang The Series\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fli>\u003Cli>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FJS2GO\">\u003Cstrong>JS2GO\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fli>\u003Cli>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FTailwind%20CSS\">\u003Cstrong>10 Ep ที่จะให้คุณเป็นมือโปร Tailwind CSS ในชั่วข้ามคืน\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003Cstrong>🔵 Facebook: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">\u003Cstrong>https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🔴 YouTube : \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.youtube.com\u002Fchannel\u002FUC2eI2RxcA2zbTqZyHyWIfRg\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>📸 Instagram: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevschool\u002F\">\u003Cstrong>Superdev Academy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🎬 TikTok: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">\u003Cstrong>https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003Cp>\u003Cstrong>🌐 Website: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdevacademy.com\u002F\">\u003Cstrong>https:\u002F\u002Fwww.superdevacademy.com\u002F\u003C\u002Fstrong>\u003C\u002Fa>\u003Cstrong>&nbsp;\u003C\u002Fstrong>\u003C\u002Fp>","41_11zon_zb5ps5und6.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002F4t8mzwj9vxlgfan\u002F41_11zon_zb5ps5und6.webp","2026-03-04 08:46:36.763Z","",{"keywords":15,"locale":63,"school_blog":73},[16,23,28,33,38,43,48,53,58],{"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:46:35.226Z","lq0dtapd8bsgln4","Concurrent I\u002FO","2026-04-10 16:13:15.930Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:44:53.062Z","puutdnxuitnxxgq","Backend","2026-04-10 16:12:51.264Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:20:33.316Z","ln1ntwattzmxo0o","programming","2026-04-10 16:07:27.299Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-03-04 08:45:30.765Z","nayuhpre9emd5xw","JS2GO","2026-04-10 16:12:59.160Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:46:34.858Z","92g4t32mafdjos0","File Handling","2026-04-10 16:13:15.837Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-03-04 08:46:35.612Z","vxhqap48dyjvi2y","I\u002FO","2026-04-10 16:13:16.023Z",{"collectionId":17,"collectionName":18,"created":49,"created_by":13,"id":50,"name":51,"updated":52,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:20:42.484Z","vslzz9nvv6n77cx","JavaScript","2026-04-10 16:07:28.339Z",{"collectionId":17,"collectionName":18,"created":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:24:40.405Z","05u3tysava2z6ga","Node.js","2026-04-10 16:07:29.504Z",{"code":64,"collectionId":65,"collectionName":66,"created":67,"flag":68,"id":69,"is_default":70,"label":71,"updated":72},"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":74,"collectionId":75,"collectionName":76,"expand":77,"id":90,"views":91},"hsa1afr8fcnd6qb","pbc_2105096300","school_blogs",{"category":78},{"blogIds":79,"collectionId":80,"collectionName":81,"created":82,"created_by":13,"id":74,"image":83,"image_alt":13,"image_path":84,"label":85,"name":36,"priority":86,"publish_at":87,"scheduled_at":13,"status":88,"updated":89,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:24:37.986Z","js2_go_2_11zon_y6paxmuz32.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fhsa1afr8fcnd6qb\u002Fjs2_go_2_11zon_y6paxmuz32.webp",{"en":36,"th":36},10,"2025-08-11 03:41:08.820Z","published","2026-04-25 02:32:14.339Z","zh79ttiv8fqsd7h",216,"4t8mzwj9vxlgfan",[20,25,30,35,40,45,50,55,60],"2025-09-16 11:39:59.806Z","เรียนรู้วิธีอ่านและเขียนไฟล์ใน JavaScript และ Go พร้อมตัวอย่างโค้ดและแนวทางปฏิบัติที่ดีที่สุด เพื่อจัดการไฟล์ได้อย่างมีประสิทธิภาพ ปลอดภัย และรองรับ concurrent I\u002FO","js2go-ep21-file-io-management-javascript-go","2026-04-25 02:48:05.955Z",1,{"th":96}]