{"id":14673,"date":"2014-07-10T10:52:46","date_gmt":"2014-07-10T05:22:46","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=14673"},"modified":"2014-07-13T21:08:42","modified_gmt":"2014-07-13T15:38:42","slug":"gofmt-formatting-the-go-code","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/gofmt-formatting-the-go-code\/","title":{"rendered":"gofmt: Formatting the go code"},"content":{"rendered":"<p><img decoding=\"async\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/golang.jpg\" alt=\"golang\" height=\"200\" class=\"aligncenter size-full wp-image-14744\" title=\"Mascot for GoLang\" \/><\/p>\n<p>When i started working on <code>golang<\/code>, some interesting things came out related to formatting. <\/p>\n<p>Till now we used to use <strong>IDE<\/strong> formatting and indentation with combos like <code>\"ctrl+shift+f\"<\/code> or something else, so!! what if your machine could take care of all the formatting issues? and what if it not only works at file level but also at package level. Need i convince you any more? \ud83d\ude09<\/p>\n<p>Let see how it happens:<\/p>\n<p><h2>gofmt<\/h2>\n<\/p>\n<p>gofmt read the go program and show the result after indentation, vertical alignment and even re formats comments too.<\/p>\n<p><strong>commands and options<\/strong><br \/>\n<code>gofmt filename<\/code> : This will print reformatted code.<br \/>\n<code>gofmt -w filename<\/code> : This will reformat the code and updates the file.<br \/>\n<code>gofmt -r 'rule' filename<\/code> : Apply the rewrite rule to the source before reformatting.<br \/>\n<code>gofmt \/path\/to\/package<\/code> : This will format the whole package.<\/p>\n<p>Here is small example for gofmt<br \/>\nfilename: <code>demo.go<\/code><\/p>\n<p>[js]<br \/>\npackage main<br \/>\n          import &quot;fmt&quot;<br \/>\n\/\/ this is demo to format code<br \/>\n            \/\/ with gofmt command<br \/>\n var a int=2;<br \/>\n             var b int=5;<br \/>\n                            var c string= `hello world`;<br \/>\n       func print(){<br \/>\n                   fmt.Println(&quot;Value for a,b and c is : &quot;);<br \/>\n                        fmt.Println(a);<br \/>\n                                 fmt.Println((b));<br \/>\n                                         fmt.Println(c);<br \/>\n                         }<br \/>\n[\/js]<\/p>\n<p><strong>Command<\/strong>: gofmt demo.go<br \/>\n<strong>Output<\/strong>:<\/p>\n<p>[js]<br \/>\npackage main<\/p>\n<p>import &quot;fmt&quot;<\/p>\n<p>\/\/ this is demo to format code<br \/>\n\/\/ with gofmt command<br \/>\nvar a int = 2<br \/>\nvar b int = 5<br \/>\nvar c string = `hello world`<\/p>\n<p>func print() {<br \/>\n\t    fmt.Println(&quot;Value for a,b and c is : &quot;)<br \/>\n        fmt.Println(a)<br \/>\n        fmt.Println((b))<br \/>\n        fmt.Println(c)<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>In above example we can see that there are multiple parenthesis around variable b,so below command will remove the extra parenthesis from println statement and will update the file too. Rule must be provide with <code>-r<\/code> option.<br \/>\n<strong>command: <\/strong> <code>gofmt -r '(a) -&gt; a' -w demo.go<\/code><br \/>\n<strong>output: <\/strong><\/p>\n<p>[js]<br \/>\npackage main<\/p>\n<p>import &quot;fmt&quot;<\/p>\n<p>\/\/ this is demo to format code<br \/>\n\/\/ with gofmt command<br \/>\nvar a int = 2<br \/>\nvar b int = 5<br \/>\nvar c string = `hello world`<\/p>\n<p>func print() {<br \/>\n\t    fmt.Println(&quot;Value for a,b and c is : &quot;)<br \/>\n        fmt.Println(a)<br \/>\n        fmt.Println(b)<br \/>\n        fmt.Println(c)<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>I hope you are liking this interesting feature of <code>golang<\/code>. Stay tuned for more interesting features of <code>golang<\/code> in upcoming blogs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Till now we used to use IDE formatting and indentation with combos like &#8220;ctrl+shift+f&#8221; or something else, so!! what if your machine could take care of all the formatting issues? and what if it not only works at file level but also at package level.<\/p>\n","protected":false},"author":66,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":29},"categories":[1],"tags":[1471,1470,398,1474],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14673"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/66"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=14673"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14673\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=14673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=14673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=14673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}