{"id":1284,"date":"2021-01-29T07:39:04","date_gmt":"2021-01-29T07:39:04","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1284"},"modified":"2021-01-29T07:39:06","modified_gmt":"2021-01-29T07:39:06","slug":"golang-program-banner","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2021\/01\/29\/golang-program-banner\/","title":{"rendered":"GoLang: Program Banner"},"content":{"rendered":"\n<p>In the last <a href=\"https:\/\/blog.samarthya.me\/wps\/2021\/01\/27\/golang-command-line-arguments\/\">blog<\/a> I talked about command line arguments <code>os.Args[]<\/code>. In this I will try and create a sample banner program that shall<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Take 3 arguments<\/li><li><code>n<\/code> &#8211; Number of times a message has to be repeated.<\/li><li><code>m<\/code> &#8211; The message that needs to be shown.<\/li><li><code>d<\/code> &#8211; If debug message needs to be shown.<\/li><\/ul>\n\n\n\n<p>I am going to use the <code><a href=\"https:\/\/golang.org\/pkg\/flag\/#pkg-overview\">flags<\/a><\/code> package.<\/p>\n\n\n\n<figure class=\"wp-block-pullquote is-style-solid-color\"><blockquote><p>Source code is available <a href=\"https:\/\/github.com\/samarthya\/gows\">here<\/a><\/p><cite>https:\/\/github.com\/samarthya<\/cite><\/blockquote><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Defining: Banner<\/h2>\n\n\n\n<p>The package <code>flag<\/code> has the extended capability of parsing the flags.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Define Flags<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Variant one<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>var times = flag.Int(\"n\", 1, \"number of times the message needs to be displayed\")\nvar msg = flag.String(\"m\", \"\", \"the message that needs to be printed\")\nvar dbg = flag.Bool(\"d\", false, \"to debug or not\")<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-pullquote is-style-solid-color\"><blockquote><p>A good reference available <a href=\"https:\/\/gobyexample.com\/command-line-flags\">here<\/a>.<\/p><\/blockquote><\/figure>\n\n\n\n<p>You can also define flags using<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var nTimes int\n\nfunc init() {\n\tflag.IntVar(&amp;nTimes, \"n\", 1, \"number of times the message needs to be displayed\")\n}<\/code><\/pre>\n\n\n\n<p>After all flags are defined you must call, to parse the command line into flags. If you miss this one you might not get the desired results. (Give it a try)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Must be called after all flags are defined and before flags are accessed within the program\nflag.Parse()<\/code><\/pre>\n\n\n\n<p>How to pass values to these flags?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-flag\n-flag=x\n-flag x<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Build<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>go build src\/cmdline\/cmd.go<\/code><\/pre>\n\n\n\n<p>This will produce the binary &#8211; <code>cmd<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Run the <code>cmd<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/cmd\n  -d    to debug or not\n  -m string\n        the message that needs to be printed\n  -n int\n        number of times the message needs to be displayed (default 1)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/cmd -n 2 -m=\"Why is world round?\"\n\n--- Command line Program ---\n\n >> Why is world round?\n\n >> Why is world round?<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/cmd -n 2 -m=\"Why is world round?\" -d=true\nDBG: Number of Arguments: 5\n--- Command line Program ---\n\n >> Why is world round?\n\n >> Why is world round?<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/cmd -m=\"Why is world round?\" -n=3  -d=true\nDBG: Number of Arguments: 4\n--- Command line Program ---\n\n >> Why is world round?\n\n >> Why is world round?\n\n >> Why is world round?<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the last blog I talked about command line arguments os.Args[]. In this I will try and create a sample banner program that shall Take 3 arguments n &#8211; Number of times a message has to be repeated. m &#8211; The message that needs to be shown. d &#8211; If debug message needs to be [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1286,"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":[150,151,23],"class_list":["post-1284","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-golang","category-technical","tag-banner","tag-flags","tag-golang","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1284","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=1284"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1284\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1286"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}