[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-js2go-ep21-file-io-management-javascript-go-all--*":3,"academy-blog-translations-2qqcx1tc8r52uum":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 File and I\u002FO Management in JavaScript and Go","sclblg987654321","school_blog_translations","\u003Cp>File management and Input\u002FOutput (I\u002FO) handling are essential skills for developers, whether you're building web applications, desktop apps, or backend systems. Efficient reading and writing of files ensures your system is stable, secure, and capable of handling large datasets.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>In this article, we compare file handling in JavaScript (Node.js) and Go, with practical code examples and best practices, so you can manage files professionally and effectively.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>I\u002FO in JavaScript (Node.js)\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Node.js provides the built-in \u003Ccode inline=\"\">fs\u003C\u002Fcode> module for file operations, supporting both synchronous and asynchronous workflows.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>Reading a File Synchronously\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>Reading a File Asynchronously\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>Writing a File\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>Advantages of Node.js I\u002FO\u003C\u002Fh3>\u003Cul>\u003Cli>Supports asynchronous operations, avoiding blocking the event loop.\u003C\u002Fli>\u003Cli>Large ecosystem with additional modules like \u003Ccode inline=\"\">fs-extra\u003C\u002Fcode> for extended functionality.\u003C\u002Fli>\u003Cli>Easy and flexible to write.\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>Limitations\u003C\u002Fh3>\u003Cul>\u003Cli>Handling very large files may require careful memory management.\u003C\u002Fli>\u003Cli>Asynchronous code can be tricky for beginners.\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>I\u002FO in Go\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>Go provides \u003Ccode inline=\"\">os\u003C\u002Fcode> and \u003Ccode inline=\"\">io\u002Fioutil\u003C\u002Fcode> \u002F \u003Ccode inline=\"\">io\u003C\u002Fcode> packages for file and I\u002FO operations.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>Reading a File\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>Writing a File\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>Advantages of Go I\u002FO\u003C\u002Fh3>\u003Cul>\u003Cli>Type-safe with clear error handling.\u003C\u002Fli>\u003Cli>Handles large files and concurrent I\u002FO efficiently.\u003C\u002Fli>\u003Cli>Built-in libraries cover most filesystem needs.\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch3>Limitations\u003C\u002Fh3>\u003Cul>\u003Cli>Syntax can be verbose for simple tasks.\u003C\u002Fli>\u003Cli>Every I\u002FO operation requires error handling; errors cannot be ignored.\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Best Practices\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cul>\u003Cli>Always handle errors\u003Cul>\u003Cli>JavaScript: \u003Ccode inline=\"\">try\u002Fcatch\u003C\u002Fcode> or callback error checks\u003C\u002Fli>\u003Cli>Go: check \u003Ccode inline=\"\">err\u003C\u002Fcode> after every I\u002FO operation\u003C\u002Fli>\u003C\u002Ful>\u003C\u002Fli>\u003Cli>Choose between synchronous or asynchronous operations wisely\u003Cul>\u003Cli>Node.js: use \u003Ccode inline=\"\">async\u002Fawait\u003C\u002Fcode> or callbacks to avoid blocking the server\u003C\u002Fli>\u003Cli>Go: use goroutines or channels for concurrent I\u002FO\u003C\u002Fli>\u003C\u002Ful>\u003C\u002Fli>\u003Cli>Be cautious with large files\u003Cul>\u003Cli>Use streaming or buffered reads\u002Fwrites instead of loading everything into memory\u003C\u002Fli>\u003C\u002Ful>\u003C\u002Fli>\u003Cli>Set proper file permissions\u003Cul>\u003Cli>Go: \u003Ccode inline=\"\">0644\u003C\u002Fcode>, \u003Ccode inline=\"\">0755\u003C\u002Fcode> when writing files\u003C\u002Fli>\u003Cli>Node.js: specify \u003Ccode inline=\"\">mode\u003C\u002Fcode> in \u003Ccode inline=\"\">writeFile\u003C\u002Fcode> if needed\u003C\u002Fli>\u003C\u002Ful>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Chr>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Comparison Summary\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>\u003Ch2>Recommendation\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cul>\u003Cli>For web servers or event-driven I\u002FO, Node.js is more flexible and easier to use with asynchronous workflows.\u003C\u002Fli>\u003Cli>For backend services, performance-critical tasks, or concurrent I\u002FO, Go provides more reliability, speed, and type safety.\u003C\u002Fli>\u003C\u002Ful>\u003Cp>&nbsp;\u003C\u002Fp>\u003Ch2>Next Episode\u003C\u002Fh2>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>In EP.22 of the JS2GO series, we will explore working with JSON in JavaScript vs Go. You’ll learn about JSON parsing, serialization, and best practices to efficiently read, write, and manipulate JSON data.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp data-start=\"498\" data-end=\"834\">\u003Cstrong>Read more\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FGolang\">\u003Cstrong>Golang The Series\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fblogs\u002Fcategories\u002FJS2GO\">\u003Cstrong>JS2GO\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp data-start=\"498\" data-end=\"834\">\u003Ca target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fwww.superdev.school\u002Fen\u002Fblogs\u002Fcategories\u002FTailwind%20CSS\">\u003Cstrong>10 Eps That Will Make You a Pro Tailwind CSS Overnight\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\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>Superdev Academy\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\u002Fen\">\u003Cstrong>https:\u002F\u002Fwww.superdevacademy.com\u002Fen\u003C\u002Fstrong>\u003C\u002Fa>\u003Cstrong>&nbsp;\u003C\u002Fstrong>\u003C\u002Fp>","42_11zon_rukrd2v2j0.webp","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002F63amimjzco78an6\u002F42_11zon_rukrd2v2j0.webp","2026-03-04 08:46:36.643Z","",{"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:44:53.062Z","puutdnxuitnxxgq","Backend","2026-04-10 16:12:51.264Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:20:33.316Z","ln1ntwattzmxo0o","programming","2026-04-10 16:07:27.299Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-04 08:45:30.765Z","nayuhpre9emd5xw","JS2GO","2026-04-10 16:12:59.160Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"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":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-03-04 08:46:35.226Z","lq0dtapd8bsgln4","Concurrent I\u002FO","2026-04-10 16:13:15.930Z",{"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:24:40.405Z","05u3tysava2z6ga","Node.js","2026-04-10 16:07:29.504Z",{"collectionId":17,"collectionName":18,"created":54,"created_by":13,"id":55,"name":56,"updated":57,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-04-10 16:07:25.893Z",{"collectionId":17,"collectionName":18,"created":59,"created_by":13,"id":60,"name":61,"updated":62,"updated_by":13},"2026-03-04 08:20:42.484Z","vslzz9nvv6n77cx","JavaScript","2026-04-10 16:07:28.339Z",{"code":64,"collectionId":65,"collectionName":66,"created":67,"flag":68,"id":69,"is_default":70,"label":71,"updated":72},"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":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":31,"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":31,"th":31},10,"2025-08-11 03:41:08.820Z","published","2026-04-25 02:32:14.339Z","2qqcx1tc8r52uum",208,"63amimjzco78an6",[20,25,30,35,40,45,50,55,60],"2025-09-16 11:40:03.129Z","Learn how to read and write files in JavaScript and Go with practical code examples and best practices, enabling efficient, safe, and concurrent I\u002FO handling.","js2go-ep21-file-io-management-javascript-go","2026-04-25 02:48:05.887Z",1,{"en":96}]