{"id":185,"date":"2018-11-08T17:57:15","date_gmt":"2018-11-08T17:57:15","guid":{"rendered":"http:\/\/kabiliravi.com\/?page_id=185"},"modified":"2019-07-21T13:48:23","modified_gmt":"2019-07-21T13:48:23","slug":"helloworld-using-glide","status":"publish","type":"page","link":"http:\/\/kabiliravi.com\/index.php\/software\/programming\/my-go-tutorial\/helloworld-using-glide\/","title":{"rendered":"Write Hello World Application using Glide"},"content":{"rendered":"<ol>\n<li>Create a folder under $GOPATH\/src\/ like this and go to that directory:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$ cd $GOPATH\/src\/\r\n$ mkdir -p github.com\/gotutorial\/helloworld\/helloworld-glide \r\n$ cd github.com\/gotutorial\/helloworld\/helloworld-glide<\/pre>\n<\/li>\n<li>Initiate a glide yaml file called <strong>glide.yaml<\/strong> inside that directory using an editor.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$ vi glide.yaml<\/pre>\n<p>And add following line into it:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package: github.com\/gotutorial\/helloworld\/helloworld-glide\r\nimport:\r\n- package: github.com\/dustin\/go-humanize<\/pre>\n<p>The first line <strong>package: github.com\/gotutorial\/helloworld<\/strong> is defining your project package so that if another project is going to have dependency to your project or library they will use this package path there.<\/p>\n<p>The second and third lines is related to your imported dependencies. I am using a library called&nbsp;<strong>go-humanize<\/strong> to format numbers is human readable format like 1024 (bytes) can be displayed as 1MB.&nbsp;<\/li>\n<li>The next step is creating your application starting point file. Usually I name it <strong>main.go<\/strong>, but you can name it whatever you like. I am fan of vi CLI editor tool to create and modify files.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$ vi main.go<\/pre>\n<p>Add following codes into main.go file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">package main\r\n\r\nimport humanize \"github.com\/dustin\/go-humanize\"\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n  fmt.Println(\"Hello World !!\")\r\n  fmt.Printf(\"Sample File Size in MB: %s.\\n\", humanize.Bytes(48234496)) \/\/ Sample File Size in MB: 46MB\r\n}<\/pre>\n<\/li>\n<li>Using glide install you can install your project dependencies. Technically, it installs the dependencies into a folder beside glide.yaml file called <strong><strong>vendor<\/strong><\/strong>&nbsp;\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$ glide install<\/pre>\n<p>You should be able to see following lines:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">[INFO]\tLock file (glide.lock) does not exist. Performing update.\r\n[INFO]\tDownloading dependencies. Please wait...\r\n[INFO]\t--&gt; Fetching updates for github.com\/dustin\/go-humanize\r\n[INFO]\tResolving imports\r\n[INFO]\tDownloading dependencies. Please wait...\r\n[INFO]\tSetting references for remaining imports\r\n[INFO]\tExporting resolved dependencies...\r\n[INFO]\t--&gt; Exporting github.com\/dustin\/go-humanize\r\n[INFO]\tReplacing existing vendor dependencies\r\n[INFO]\tProject relies on 1 dependencies.<\/pre>\n<p>&nbsp;<\/li>\n<li>If you list the files on the root directory of your project, you should be able to find a file called <strong><strong><strong>glide.lock <\/strong><\/strong><\/strong>and a folder called <strong><strong><strong>vendor&nbsp;<\/strong><\/strong><\/strong>which has your project dependencies files <strong><strong><br \/>\n<\/strong><\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">$ ls\r\nglide.lock  glide.yaml  main.go  vendor<\/pre>\n<p>&nbsp;Go inside the <strong>vendor<\/strong> folder and list the files using <strong>ls<\/strong> command to see how <strong>glide install<\/strong> downloads and stores the dependencies you are referring in <strong>import<\/strong> tag inside <strong>glide.yaml<\/strong> file<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$ cd vendor\r\n$ ls\r\ngithub.com\r\n$ cd github.com\r\n$ ls\r\ndustin\r\n$ cd dustin\r\n$ ls\r\ngo-humanize\r\n$ cd go-humanize\r\n$ ls\r\nLICENSE          bigbytes.go       bytes_test.go  commaf.go       english       humanize.go     ordinals.go       si_test.go\r\nREADME.markdown  bigbytes_test.go  comma.go       commaf_test.go  ftoa.go       number.go       ordinals_test.go  times.go\r\nbig.go           bytes.go          comma_test.go  common_test.go  ftoa_test.go  number_test.go  si.go             times_test.go\r\n\r\n<\/pre>\n<p>Go back to your project root<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$ cd ..\/..\/..\/..\/<\/pre>\n<\/li>\n<li>Now you can run following command to build your app.&nbsp;You should be able to find an executable file named <strong>myapp&nbsp;<\/strong>in your project root directory\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$ go build -o myapp\r\n\r\nglide.lock  glide.yaml  main.go  myapp  vendor<\/pre>\n<\/li>\n<li>Finally, run <strong>myapp<\/strong> like this. You should be able to see &#8216;Hello World !!&#8217; prompt in the first line and &#8216;Sample File Size in MB: 48 MB.&#8217; in the second line\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">$ .\/myapp\r\nHello World !!\r\nSample File Size in MB: 48 MB.<\/pre>\n<p>&nbsp;<\/p>\n<p>You can find the source code in my github here:<br \/>\n<a href=\"https:\/\/github.com\/gotutorial\/helloworld.git\">https:\/\/github.com\/gotutorial\/helloworld.git<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Create a folder under $GOPATH\/src\/ like this and go to that directory: $ cd $GOPATH\/src\/ $ mkdir -p github.com\/gotutorial\/helloworld\/helloworld-glide $ cd github.com\/gotutorial\/helloworld\/helloworld-glide Initiate a glide yaml file called glide.yaml inside that directory using an editor. $ vi glide.yaml And add following line into it: package: github.com\/gotutorial\/helloworld\/helloworld-glide import: &#8211; package: github.com\/dustin\/go-humanize The first line package: github.com\/gotutorial\/helloworld [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":131,"menu_order":4,"comment_status":"open","ping_status":"closed","template":"","meta":{"ngg_post_thumbnail":0,"footnotes":""},"class_list":["post-185","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":11,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/185\/revisions"}],"predecessor-version":[{"id":221,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/185\/revisions\/221"}],"up":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/131"}],"wp:attachment":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/media?parent=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}