{"id":2549,"date":"2021-11-15T02:50:12","date_gmt":"2021-11-15T02:50:12","guid":{"rendered":"https:\/\/dft.wiki\/?p=2549"},"modified":"2022-02-07T02:31:06","modified_gmt":"2022-02-07T02:31:06","slug":"golang-go-cheat-cheet","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=2549","title":{"rendered":"Golang (GO) Cheat Cheet"},"content":{"rendered":"<p>Golang or GO is a programming language developed in 2009 by three Google engineers to fulfill its high-performance needs.<\/p>\n<p>Its main goal is performance, which can be achieved by combining simplicity with native parallelism (multi-threading and multi-processing).<\/p>\n<p>The main source of documentation is the official website [<a href=\"https:\/\/golang.org\/\">Link<\/a>] that also offers a web code test and syntax check [<a href=\"https:\/\/play.golang.org\/\">Link<\/a>].<\/p>\n<p>For developing using VS Code [<a href=\"https:\/\/code.visualstudio.com\/\">Link<\/a>], consider installing the extension GO, by Lukehoban [<a href=\"https:\/\/code.visualstudio.com\/blogs\/2020\/06\/09\/go-extension\">Link<\/a>].<\/p>\n<p>Installing Go compiler:<\/p>\n<pre>sudo apt-get install golang -y<\/pre>\n<p>Or directly from the source by creating a Bash script with the following content:<\/p>\n<pre>export GOLANG=\"$(curl https:\/\/golang.org\/dl\/|grep linux-armv6l|grep -v beta|head -1|awk -F\\&gt; {'print $3'}|awk -F\\&lt; {'print $1'})\"\r\nwget https:\/\/golang.org\/dl\/$GOLANG\r\nsudo tar -C \/usr\/local -xzf $GOLANG\r\nrm $GOLANG\r\nunset GOLANG<\/pre>\n<p>Commands for running, testing for data race, building, installing, and downloading external code:<\/p>\n<pre>go run src\/program.go\r\ngo run -race src\/program.go\r\ngo build src\/program.go\r\ngo install src\/program.go\r\ngo get github.com\/nsf\/gocode<\/pre>\n<hr \/>\n<p><strong>KEEP IN MIND<\/strong><\/p>\n<ul>\n<li>GO defines variables, functions, etc as GLOBAL when the first letter of the name is capitalized, and exclusive in the context if in lower case.<\/li>\n<li>The manipulation of strings is not straightforward and will require importing the module<code>strconv<\/code> [<a href=\"https:\/\/pkg.go.dev\/strconv\">Link<\/a>]. Otherwise, you will have to deal with the UTF-8 representation number of each character.<\/li>\n<li>GO compilation is very fast and the output binary contains everything it needs to run. No additional file is required.<\/li>\n<li>It is made for creating applications that run in servers and do not support officially any kind of GUI.<\/li>\n<li>Each project directory will be as follows:\n<ul>\n<li><strong>ProjectName<\/strong> (the root of the project)\n<ul>\n<li><strong>src<\/strong> (source code)\n<ul>\n<li>The <code>.go<\/code> files must be located here.<\/li>\n<li>Including the downloaded codes from <code>go get<\/code>.<\/li>\n<\/ul>\n<\/li>\n<li><strong>bin<\/strong> (installed programs)\n<ul>\n<li>Compiled files will be placed here is used <code>go install<\/code>.<\/li>\n<\/ul>\n<\/li>\n<li><strong>pkg<\/strong>\n<ul>\n<li><strong>mod<\/strong>\n<ul>\n<li>Additional packets and modules will be stored here.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<p>DECLARATION<\/p>\n<ul>\n<li>const varName int<\/li>\n<li>var varName int = 10<\/li>\n<li>var varName1, varName2 = 10, 20<\/li>\n<\/ul>\n<p>VARIABLE TYPES<\/p>\n<ul>\n<li>bool<\/li>\n<li>string<\/li>\n<li>int int8 int16 int32 int64<\/li>\n<li>uint uint8 uint16 uint32 uint64 uintptr<\/li>\n<li>byte<\/li>\n<li>rune<\/li>\n<li>float32 float64<\/li>\n<li>complex64 complex128<\/li>\n<\/ul>\n<p>LOGICAL OPERATORS<\/p>\n<ul>\n<li>&amp;&amp;<\/li>\n<li>||<\/li>\n<li>!<\/li>\n<\/ul>\n<p>COMPARISON OPERATORS<\/p>\n<ul>\n<li>++<\/li>\n<li>!=<\/li>\n<li>&lt;<\/li>\n<li>&lt;=<\/li>\n<li>&gt;<\/li>\n<li>&gt;=<\/li>\n<\/ul>\n<p>ARITHMETIC OPERATORS<\/p>\n<ul>\n<li>+<\/li>\n<li>&#8211;<\/li>\n<li>*<\/li>\n<li>\/<\/li>\n<li>%<\/li>\n<\/ul>\n<p>BIT OPERATORS<\/p>\n<ul>\n<li>&amp;<\/li>\n<li>|<\/li>\n<li>^<\/li>\n<li>&amp;^<\/li>\n<li>&lt;&lt;<\/li>\n<li>&gt;&gt;<\/li>\n<\/ul>\n<hr \/>\n<p><strong>ARRAYS, SLICES, and RANGES<\/strong><\/p>\n<pre>var arrayName = [3]int{1, 2, 3}\r\nvar sliceNameA = arrayName[1:2]\r\nvar sliceNameB = []int\r\nsliceNameB = append(sliceName, 10, 20)\r\nsliceNameC = append(sliceNameA, sliceNameB)<\/pre>\n<hr \/>\n<p><strong>SIMPLIFIED DECLARATION<\/strong><\/p>\n<pre>varName := 1\r\nvarName := 1.5\r\nvarName := 1 + 2i\r\nstrName := \"String\"\r\nstrName := `Multiline\r\nstring`<\/pre>\n<hr \/>\n<p><strong>IMPORT<\/strong><\/p>\n<pre>import \"fmt\"<\/pre>\n<pre>import (\r\n  \"math\/rand\"\r\n  \"embed\"\r\n  \"log\"\r\n  \"net\/http\"\r\n  \"io\"\r\n)<\/pre>\n<hr \/>\n<p>&nbsp;<\/p>\n<p><strong>POINTERS<\/strong><\/p>\n<pre>varName1 = 123\r\nvarName2 = &amp;varName1 \/\/ refer to the memory address\r\nvarName3 = *varName2 \/\/ derefer value in the pointer address<\/pre>\n<hr \/>\n<p><strong>PRINT<\/strong><\/p>\n<pre>fmt.Println(\"First line\", \"Second line\")<\/pre>\n<pre>fmt.Printf(\"%d hex:%x bin:%b fp:%f sci:%e\",11,11,11,11.0,11.0)<\/pre>\n<hr \/>\n<p><strong>IF, ELSE IF, and ELSE<\/strong><\/p>\n<pre>if varName == \"A\" || varName == \"B\" {\r\n  \/\/ code\r\n} else if varName == \"C\" {\r\n  \/\/ code\r\n} else {\r\n  \/\/ code\r\n}\r\n<\/pre>\n<hr \/>\n<p><strong>SWITCH<\/strong><\/p>\n<pre>switch varName {\r\n  case \"A\":\r\n    \/\/ code\r\n    fallthrough\r\n  case \"B\":\r\n    \/\/ code\r\n  default:\r\n    \/\/ code\r\n}<\/pre>\n<hr \/>\n<p><strong>FOR LOOPS<\/strong><\/p>\n<pre>for i != 50 {\r\n  \/\/ code\r\n  i ++\r\n}<\/pre>\n<pre>for i := 0; i &lt;= 10; i++ {\r\n  \/\/ code\r\n}<\/pre>\n<hr \/>\n<p><strong>FUNCTION<\/strong><\/p>\n<pre>func functionName(argName int) int {\r\n  \/\/ code\r\n  return\r\n}<\/pre>\n<p><strong>ANONYMOUS FUNCTION<\/strong><\/p>\n<pre>Anonnymous Function\r\nfunc (){\r\n  \/\/ code\r\n}<\/pre>\n<p><strong>VARIADIC FUNCTION<\/strong><\/p>\n<pre>func functionName(args ...int) (string, int) {\r\n  for _, v := range args {\r\n    \/\/ code\r\n  }\r\n  return strName varIntName\r\n}<\/pre>\n<hr \/>\n<p><strong>HELLO WORLD<\/strong><\/p>\n<pre>package main \r\n\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n   fmt.Println(\"Hello World!\")\r\n}\r\n<\/pre>\n<hr \/>\n<p><strong>CHANNELS<\/strong><\/p>\n<pre>packet main\r\n\r\nimport \"fmt\"\r\nimport \"sync\"\r\n\r\nvar waitGroup = sync.WaitGroup{}\r\n\r\nfunc main {\r\n  channelName := make(chan int)\r\n  waitGroup.Add(2)\r\n  go func(){\r\n    i := &lt;- channelName\r\n    fmt.Println(i)\r\n    waitGroup.Done()\r\n  }()\r\n  go func(){\r\n    channelName &lt;- 99\r\n    waitGroup.Done()\r\n  }()\r\n  waitGroup.Wait()\r\n}<\/pre>\n<hr \/>\n<p><strong>HTTP LISTENER<\/strong><\/p>\n<pre>package main\r\n\r\nimport \"net\/http\"\r\n\r\nfunc main() {\r\n   http.HandleFunc(\"\/\", func(w http.ResponseWriter, r *http.Request) {\r\n      w.Write([]byte(\"Welcome!\"))\r\n   })\r\n   err := http.ListenAndServe(\":80\", nil)\r\n   if err != nil {\r\n      panic(err.Error())\r\n   }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Golang or GO is a programming language developed in 2009 by three Google engineers to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2549","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2549"}],"version-history":[{"count":4,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2549\/revisions"}],"predecessor-version":[{"id":2783,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/2549\/revisions\/2783"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}