{"id":1269,"date":"2021-01-25T12:46:47","date_gmt":"2021-01-25T12:46:47","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1269"},"modified":"2021-01-25T12:46:48","modified_gmt":"2021-01-25T12:46:48","slug":"files-golang-2","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2021\/01\/25\/files-golang-2\/","title":{"rendered":"Files: GoLang"},"content":{"rendered":"<p>In the last blog, I tried <a href=\"https:\/\/blog.samarthya.me\/wps\/2021\/01\/21\/files-golang\/\">reading a file in GoLang<\/a>.<\/p>\n\n\n<figure class=\"wp-block-pullquote is-style-solid-color\"><blockquote><p>My repository in GitHub has all the code that I write (<a href=\"https:\/\/github.com\/samarthya\/valueandpointer\">link<\/a>)<\/p><\/blockquote><\/figure>\n\n\n\n<p>Now, I will extend the same program to write to the same file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Debug messages - Centralized logging\nfunc message(m string) {\n\tlog.Printf(\" FILES: %s\", m)\n}\n\nfunc fileStats(f *os.File) {\n\tmessage(\" File Name: \" + f.Name())\n\n\tfs, _ := f.Stat()\n\tmessage(\" File Mode: \" + fs.Mode().String())\n\tmessage(\" Last Mode time: \" + fs.ModTime().String())\n\tmessage(\" File Size: \" + strconv.Itoa(int(fs.Size())))\n}\n\n\/\/ WriteFile writes to the file identified by f\nfunc WriteFile(f string, m string) {\n\n\tmessage(\"--- Write Invoked --- \")\n\n\tfh, err := os.OpenFile(f, os.O_APPEND|os.O_WRONLY, os.ModeAppend)\n\n\tif err != nil {\n\t\tmessage(\" Unable to open file \" + f)\n\t\t\/\/ Conventionally, code zero indicates success\n\t\tos.Exit(1)\n\t}\n\n\tdefer fh.Close()\n\tfileStats(fh)\n\n\tmessage(\" Writing: \" + m)\n\tn, err := fh.WriteString(\"\\n\")\n\tn, err = fh.WriteString(m)\n\tfh.WriteString(\" ----- \" + string(n) + \" ----- \")\n\n\tif err == nil {\n\t\t\/\/ w.Flush()\n\t\tfmt.Println(\" -- Closed --\")\n\t\treturn\n\t}\n\n\tfmt.Println(\" Error: \" + err.Error())\n\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">What it does?<\/h2>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<ol class=\"wp-block-list\"><li>Opens a file <code>os.OpenFile(f, os.O_APPEND|os.O_WRONLY, os.ModeAppend)<\/code><\/li><li>WritesString mesage <code>m<\/code> &#8211; and captures the response <code>n, err = fh.WriteString(m)<\/code>; Where n is the number of bytes written and err is error if encountered.<\/li><li>Close the file handle <code>fh.Close()<\/code><\/li><\/ol>\n\n\n\n<p>For my sample program the readme looks like this<\/p>\n<\/div><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>I am just a plain text file\nI have float, int, bool and string\nint: 20\nfloat: 20.50\nstring: \"Get some content\"\nbool: true\n> 2021-01-25 18:01:57.48917 +0530 IST m=+0.000089785\n ----- 5 ----- \n> 2021-01-25 18:02:07.965749 +0530 IST m=+0.000071314\n ----- 6 ----- \n> 2021-01-25 18:02:10.820226 +0530 IST m=+0.000066656\n ----- 6 ----- \n> 2021-01-25 18:02:12.732445 +0530 IST m=+0.000071841\n ----- 6 ----- \n> 2021-01-25 18:02:13.782766 +0530 IST m=+0.000071555\n ----- 6 ----- \n> 2021-01-25 18:02:14.574956 +0530 IST m=+0.000085469\n ----- 6 ----- \n> 2021-01-25 18:02:15.389384 +0530 IST m=+0.000068317\n ----- 6 ----- \n> 2021-01-25 18:08:07.145537 +0530 IST m=+0.000062991\n ----- 6 ----- \n> 2021-01-25 18:08:08.814768 +0530 IST m=+0.000122495\n ----- 6 ----- \n> 2021-01-25 18:08:09.710064 +0530 IST m=+0.000092775\n ----- 6 ----- \n> 2021-01-25 18:08:10.45252 +0530 IST m=+0.000073827\n ----- 5 ----- <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the last blog, I tried reading a file in GoLang. My repository in GitHub has all the code that I write (link) Now, I will extend the same program to write to the same file. What it does? Opens a file os.OpenFile(f, os.O_APPEND|os.O_WRONLY, os.ModeAppend) WritesString mesage m &#8211; and captures the response n, err [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1271,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[137,34],"tags":[143,23],"class_list":["post-1269","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-golang","category-technical","tag-files","tag-golang","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=1269"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1269\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1271"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}