{"id":1293,"date":"2021-01-29T17:47:07","date_gmt":"2021-01-29T17:47:07","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1293"},"modified":"2021-01-29T17:47:09","modified_gmt":"2021-01-29T17:47:09","slug":"golang-hex-output","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2021\/01\/29\/golang-hex-output\/","title":{"rendered":"GoLang: Hex Output"},"content":{"rendered":"\n<p class=\"has-drop-cap\">In last few blogs I have written about reading <code>commandline<\/code> parameters reading <code>files<\/code> and using the <code>os<\/code> package.<\/p>\n\n\n\n<p>In this blog, I will combine some these items to read a file <code>byte<\/code> by <code>byte<\/code> using the <code>bufio<\/code> package <code>func<\/code>&#8216;s.<\/p>\n\n\n\n<p>Some of the things I am still learning while I try out different things is that how to organise the perfect folder structure to ensure what I learn is manageable to refer back when and if I revisit. <\/p>\n\n\n\n<pre class=\"wp-block-verse\" id=\"practice\">Still refining, but am getting better by the day.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Challenges<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Defining the flag to get the input from the command line using the <code>-f<\/code> flag.<\/li><li>If the input is not provided the format expected should be printed.<\/li><li>For a file present, read byte by byte and output <code>HEX:Byte<\/code> representation.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Defining flag<\/h2>\n\n\n\n<pre id=\"code\" class=\"wp-block-code\"><code>var fileName string\n\nfunc init() {\n\tflag.StringVar(&amp;fileName, \"f\", \"\", \"File to read\")\n\tflag.Parse()\n}<\/code><\/pre>\n\n\n\n<p>Why the <code>flag.Parse()<\/code> is defined here in <code>Init()<\/code>? Before I get there let me add some details about <code>init<\/code> and <code>Parse<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-white-color\"><p>You can read more about <strong>init<\/strong> methods <a href=\"https:\/\/golang.org\/doc\/effective_go.html#init\">here<\/a>.<\/p><\/blockquote><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What is Init()?<\/h2>\n\n\n\n<p>Each source file can define its own niladic (operator or function having no arguments) <code>init<\/code> function (one or more) to set up whatever state is required. <\/p>\n\n\n\n<p>The function <code>init<\/code> is called after all the variable declarations in the package have evaluated their initializers, and those are evaluated only after all the imported packages have been initialized.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parse<\/h2>\n\n\n\n<p>Also Parse() must be called after all flags are defined and before flags are accessed by the program.<\/p>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-white-color\"><p>Parse() (package flags) parses the command-line flags from os.Args[1:]. <\/p><cite>Official go documentation<\/cite><\/blockquote><\/figure>\n\n\n\n<p>Since it is to set state and I will be calling the main processing logic from a different source file it is best to initialise the state in <code>init()<\/code> else the flag might not be set properly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Hex:Byte<\/h2>\n\n\n\n<p>The function below are quite self-explanatory<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">OpenAndDump<\/h3>\n\n\n\n<p>This function is to open a file passed to it as an argument and uses <code>os.Open<\/code> to open the file and then a buffered reader is created to read the file byte by byte (using the method <code>dump<\/code>) and <code>fmt.Fprintf<\/code> is used to push the content to <code>Stdout<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/dump dumps the content from a reader\nfunc dump(fr *bufio.Reader) {\n\tfmt.Println(\"\\n -------- STARTOFFILE -------\")\n\tfor {\n\t\tb, err := fr.ReadByte()\n\t\tif err == io.EOF {\n\t\t\tbreak\n\t\t}\n\t\tfmt.Fprintf(os.Stdout, \" %X:%c\", b, b)\n\t}\n\tfmt.Println(\"\\n ------ ENDOFFILE ------\")\n}\n\n\/\/ OpenAndDump a file and dumps the content\nfunc OpenAndDump(f string) {\n\tfh, err := os.Open(f)\n\tif err == nil {\n\t\tdump(bufio.NewReader(fh))\n\t\tos.Exit(0)\n\t}\n\n\tdefer fh.Close()\n\tfmt.Fprintf(os.Stderr, \"ERR: (%s) %s\", f, err.Error())\n\treturn\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Entry function: <code>Cmd()<\/code><\/h2>\n\n\n\n<p>It does the following<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Checks for the number of flags (<a href=\"https:\/\/golang.org\/pkg\/flag\/#NFlag\">NFlag<\/a>) passed, if 0 then prints the <a href=\"https:\/\/golang.org\/pkg\/flag\/#PrintDefaults\">default<\/a> usage and exits with error.<\/li><li>If argument <code>-f<\/code> is set then invoke the <code>OpenAndDump<\/code> method defined.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>const (\n\t\/\/ InvalidArgument for the program\n\tInvalidArgument = \"Please provide a valid file\\n\"\n)\n\n\/\/ Cmd Runs the command\nfunc Cmd() {\n\t\/\/ https:\/\/golang.org\/pkg\/flag\/#NArg\n\t\/\/ fmt.Fprintf(os.Stdout, \"DBG: %d %s\\n\", flag.NArg(), fileName)\n\n\tif flag.NFlag() == 0 {\n\t\tfmt.Fprintf(os.Stderr, \"%s\", InvalidArgument)\n\t\tflag.PrintDefaults()\n\t\tos.Exit(1)\n\t}\n\n\t\/\/OpenAndDump(flag.Arg(i))\n\tOpenAndDump(fileName)\n\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Sample Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/main -f filer.txt<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">File: filer.txt<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>I am a sample file.\nI have a new line.\nI even have an int 10.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>-------- STARTOFFILE -------\n 49:I 20:  61:a 6D:m 20:  61:a 20:  73:s 61:a 6D:m 70:p 6C:l 65:e 20:  66:f 69:i 6C:l 65:e 2E:. A:\n 49:I 20:  68:h 61:a 76:v 65:e 20:  61:a 20:  6E:n 65:e 77:w 20:  6C:l 69:i 6E:n 65:e 2E:. A:\n 49:I 20:  65:e 76:v 65:e 6E:n 20:  68:h 61:a 76:v 65:e 20:  61:a 6E:n 20:  69:i 6E:n 74:t 20:  31:1 30:0 2E:.\n ------ ENDOFFILE ------<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In last few blogs I have written about reading commandline parameters reading files and using the os package. In this blog, I will combine some these items to read a file byte by byte using the bufio package func&#8216;s. Some of the things I am still learning while I try out different things is that [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1295,"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,151,152,23],"class_list":["post-1293","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-golang","category-technical","tag-files","tag-flags","tag-fmt","tag-golang","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1293","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=1293"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1293\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1295"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}