[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blog-translations-none":3,"academy-blogs-en-1-1-all-go-file-handling-reading-writing-all--*":4},{},{"data":5,"meta":73},[6],{"categoryId":7,"collectionId":8,"collectionName":9,"content":10,"createBy":11,"createDate":12,"created":13,"description":14,"expand":15,"group":65,"id":65,"image":66,"imageAlt":67,"imagePath":68,"keywordIds":69,"langId":61,"publishDate":36,"scheduleDate":12,"slug":70,"status":28,"title":67,"updateBy":11,"updated":71,"views":72},"wqxt7ag2gn7xcmk","sclblg987654321","school_blog","\u003Cp>\u003Cspan style=\"font-size:20px;\">\u003Cstrong>Go and File Handling: Reading and Writing Files Made Easy!\u003C\u002Fstrong>\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>File handling is a fundamental aspect of good programming. Today, we will teach you how to read and write files simply with Go!\u003C\u002Fp>\u003Cp>\u003Cbr>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>1. What is File Handling in Go?\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>File handling is an essential part of software development, such as saving data to a file or reading data from a file for use in a program.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>2. Writing Files with ioutil.WriteFile()\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>The easiest way to write data to a file:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"io\u002Fioutil\"\r\n    \"log\"\r\n)\r\n\r\nfunc main() {\r\n    content := []byte(\"Hello, Go!\")\r\n    err := ioutil.WriteFile(\"example.txt\", content, 0644)\r\n    if err != nil {\r\n        log.Fatal(err)\r\n    }\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>Note: 0644 is the file permission that allows everyone to read the file, but only the owner can modify it.\u003C\u002Fp>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>3. Reading Files with ioutil.ReadFile()\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>Let's read data from the file we wrote:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n    \"io\u002Fioutil\"\r\n    \"log\"\r\n)\r\n\r\nfunc main() {\r\n    content, err := ioutil.ReadFile(\"example.txt\")\r\n    if err != nil {\r\n        log.Fatal(err)\r\n    }\r\n    fmt.Println(string(content))\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>4. Using os for Safe File Opening and Closing\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>Use the os package for more flexible file management and use defer to close the file:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"log\"\r\n    \"os\"\r\n)\r\n\r\nfunc main() {\r\n    file, err := os.Open(\"example.txt\")\r\n    if err != nil {\r\n        log.Fatal(err)\r\n    }\r\n    defer file.Close()\r\n\r\n    log.Println(\"File opened successfully\")\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>&nbsp;\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size:18px;\">\u003Cstrong>5. Appending to Files\u003C\u002Fstrong>\u003C\u002Fspan>\u003Cbr>Sometimes you want to add data to the end of an existing file:\u003C\u002Fp>\u003Cpre>\u003Ccode class=\"language-plaintext\">package main\r\n\r\nimport (\r\n    \"os\"\r\n)\r\n\r\nfunc main() {\r\n    f, err := os.OpenFile(\"example.txt\", os.O_APPEND|os.O_WRONLY, 0644)\r\n    if err != nil {\r\n        panic(err)\r\n    }\r\n    defer f.Close()\r\n\r\n    if _, err := f.WriteString(\"\\nAppended content!\"); err != nil {\r\n        panic(err)\r\n    }\r\n}\r\u003C\u002Fcode>\u003C\u002Fpre>","r8v4zgsahjuwpeb","","2026-03-04 08:51:51.732Z","Learn how to write and read files in Go, including safely opening and closing files, and appending data.",{"categoryId":16,"keywordIds":30,"langId":56},{"blogIds":17,"collectionId":18,"collectionName":19,"createBy":20,"created":21,"id":7,"image":22,"imageAlt":12,"imagePath":23,"label":24,"name":25,"priority":26,"publishDate":27,"scheduleDate":12,"status":28,"updateBy":20,"updated":29},[],"sclcatblg987654321","school_category_blog","oplnwslvnmx5axc","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw.png",{"en":25,"th":25},"Golang The Series",1,"2026-03-16 04:39:38.440Z","Publish","2026-03-17 06:07:59.733Z",[31,38,42,46,51],{"collectionId":32,"collectionName":33,"createBy":12,"created":34,"id":35,"publishDate":36,"scheduleDate":12,"status":28,"title":37,"updateBy":12,"updated":34},"sclkey987654321","school_keyword","2026-03-04 08:51:50.254Z","6nkk61dvy4u5pjr","2025-01-27 04:32:52.239Z","Append",{"collectionId":32,"collectionName":33,"createBy":12,"created":39,"id":40,"publishDate":36,"scheduleDate":12,"status":28,"title":41,"updateBy":12,"updated":39},"2026-03-04 08:51:50.542Z","ut75ptasgqhi8jp","ReadFile",{"collectionId":32,"collectionName":33,"createBy":12,"created":43,"id":44,"publishDate":36,"scheduleDate":12,"status":28,"title":45,"updateBy":12,"updated":43},"2026-03-04 08:51:51.198Z","ez61k8wbi1s3xby","WriteFile",{"collectionId":32,"collectionName":33,"createBy":12,"created":47,"id":48,"publishDate":49,"scheduleDate":12,"status":28,"title":50,"updateBy":12,"updated":47},"2026-03-04 08:46:34.858Z","92g4t32mafdjos0","2025-09-16 11:40:03.129Z","File Handling",{"collectionId":32,"collectionName":33,"createBy":12,"created":52,"id":53,"publishDate":54,"scheduleDate":12,"status":28,"title":55,"updateBy":12,"updated":52},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","2026-01-28 00:54:28.566Z","Go",{"code":57,"collectionId":58,"collectionName":59,"createAt":60,"id":61,"is_default":62,"language":63,"updateAt":64},"en","pbc_1989393366","locale","2026-01-22 11:00:02.726Z","qv9c1llfov2d88z",false,"English","2026-02-05 10:48:59.032Z","5uvchbcjgfqbyjv","24_11zon_sfu661phtn.webp","EP.12 Go and File Handling: Easy File Reading and Writing!","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002F5uvchbcjgfqbyjv\u002F24_11zon_sfu661phtn.webp",[35,40,44,48,53],"go-file-handling-reading-writing","2026-03-04 08:51:52.049Z",230,{"pagination":74},{"page":26,"pageSize":26,"pageCount":26,"total":26}]