{"id":553,"date":"2022-09-01T20:56:05","date_gmt":"2022-09-01T20:56:05","guid":{"rendered":"https:\/\/melfaeforest.xhere.eu.org\/?p=553"},"modified":"2022-09-06T15:41:57","modified_gmt":"2022-09-06T15:41:57","slug":"learning-go-language-day-4","status":"publish","type":"post","link":"https:\/\/melfaeforest.xhere.eu.org\/?p=553","title":{"rendered":"Learning Go Language &#8211; Day 4"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><strong>If-Statements<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">If-Statements:\n- Initializers: Allows you to generate a statement to set-up for a comparison\n- Variables in an if-statement are block-scoped\n- Comparison operators (primarily for numeric types):\n  - less-than (&lt;)\n  - greater-than (&gt;)\n  - less-than-or-equal-to (&lt;=)\n  - greater-than-or-equal-to (&gt;=)\n  - double-equals operator (==)\n  - not-equals operator (!=)\n  \n\n- Logical operators:\n  - And (&amp;&amp;)\n  - Or (||)\n  - Not (!)\n\n- Short Circuiting: Where Go-Lang skips comparison calculations\n  - It chains tests with logical operators for an outcome\n  - If an Or (||) operate evaluates a test to be True, it stops the comparison there\n  - If an And (&amp;&amp;) operate evaluates a test to be False, it stops the comparison there\n\n- Equality &amp; Floats: Better convert those floats because they don't play nice with comparisons!<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">func main() {\n  ygoDetails := map[string]string {\n    \"Name\": \"Xie\",\n    \"DeckName\": \"Manipulstring\",\n    \"Dimension\": \"Xyz Dimension\",\n  }\n\n  \/\/ Example #1\n  if true {\n    fmt.Println(\"The test is true!\")\n  }\n\n\n  \/\/ Example #2\n  if pop, ok := ygoDetails[\"Name\"]; ok {\n    fmt.Println(pop)\n  }\n  \/\/ Below line of code will cause an error because variables are block-scoped\n  \/\/ fmt.Println(pop)\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Looping<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Loops:\n- For statements: Simple l00ps:\n  - for initializer; test; increment {}\n    - for test {}\n    - for {}\nNOTE: The iteration variables in a loop are scoped to that block\n\n- Exiting early:\n  - break (breaks out of a loop entirely)\n    - continue (skips an interation of a loop)\n    - labels (Allows you to use break out from an outer loop)\n\n- Looping over collections:\n  - Arrays, slices, maps, strings, and channels can be looped through\n    - NOTE: Channels are a special case\n  - \"for k, v := range collection {}\" to get the key\/value of an item in a collection\n    - Use \"_\" (underscore) as a throwaway variable to get EITHER the key\/value<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Example 1: Basic loop\nfunc main() {\n  for i := 0; i &lt; 5; i++ {\n    fmt.Println(i)\n  }\n\n  \/\/ You can also change the increment value\n  for i := 0; i &lt; 5; i = i+2 {\n    fmt.Println(i)\n  }\n\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Example 2: Having the initializer be outside the loop\n\/\/ NOTE: You still need the first semi-colon when you do this\nfunc main() {\n  i := 0\n  for ; i &lt; 5; i++ {\n    fmt.Println(i)\n  }\n\n\/\/ You can go further and put the incremental in the loop itself\n\/\/ If you do, and you remove the initalizer too, you can remove the semi-colons completely\n  i = 0\n  for i &lt; 10 {\n    fmt.Println(i)\n    i++\n  }\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Example 3: Simplifying EVERYTHING\nfunc main() {\n  i = 0\n  for {\n    fmt.Println(i)\n    i++\n\t\t\n    \/\/ WARNING: Without this break checker, this loop will run forever\n    if i == 5 {\n      break\n    }\n  }\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Example 4: Continuing on\nfunc main() {\n  for i := 10; i &lt; 20; i++ {\n    if i%2 == 0 {\n      continue\n    }\n  fmt.Println(i)\n  }\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Example 5: LABEL IT\nfunc main() {\nLoop:\n  for i := 1; i &lt;= 3; i++ {\n    for j := i; j &lt;= 3; j++ {\n      fmt.Println(i * j)\n      if i * j &gt;= 3 {\n        break Loop\n      }\n    }\n  }\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Switches<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">- Initializers: Allows you to generate a statement to set-up for a comparison\n - Switching on a tag\n - Cases with multiple tests\n   - There can be NO OVERLAP in testing cases\n - Switches with no tags\n   - There CAN be overlap in testing cases\n   - The first case that evaluates to True gets used if there is overlap\n\n - Fallthrough keyword\n   - While most other languages have explicit breaks and implicit fallthrough,\n   - Go has the reverse: Implicit breaks and explicit fallthroughs\n   - Keep in mind that using fallthrough will execute anything below it, regardless of any calculations\n- Type switches: To get the underlying types<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Example #1\nfunc main() {\n  switch 5 {\n  case 1:\n    fmt.Println(\"one\")\n  case 2:\n    fmt.Println(\"two\")\n  default:\n    fmt.Println(\"not one or two\")\n  }\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Example #2\nfunc main() {\nswitch 5 {\n  case 1, 5, 10:\n    fmt.Println(\"one, five, or ten\")\n  case 2, 4, 6:\n    fmt.Println(\"two, four, or six\")\n  default:\n    fmt.Println(\"another number\")\n  }\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Example #3\nfunc main() {\nswitch i := 2 + 4; i {\n  case 1, 5, 10:\n    fmt.Println(\"one, five, or ten\")\n  case 2, 4, 6:\n    fmt.Println(\"two, four, or six\")\n  default:\n    fmt.Println(\"another number\")\n  }\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Example #4: Tagless syntax\nfunc main() {\n  i := 10\n  switch {\n    case i &lt;= 10:\n    fmt.Println(\"less than or equal to ten\")\n\n    \/\/ Will force the below case to execute regardless of logic\n    \/\/ fallthrough\n  case i &lt;= 20:\n    fmt.Println(\"less than or equal to twenty\")\n  default:\n    fmt.Println(\"greater than twenty\")\n  }\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Example #5: Type switching\nfunc main() {\n  var e interface{} = 1 \/\/ An int\n  switch e.(type) {\n    case int:\n      fmt.Println(\"e is an int\")\n    case float64:\n      fmt.Println(\"e is a float64\")\n    case string:\n      fmt.Println(\"e is string\")\n    default:\n      fmt.Println(\"e is another type\")\n  }\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-default\"\/>\n\n\n\n<p><strong>Defer, Panic, and Recover<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Defer:\n- Used to delay execution of a statement until function exits\n- Useful to group \"open\" and \"close\" functions together\n  - Be careful in loops\n- Runs in LIFO (last-in, first-out) order\n- Arguments evaluated at time defer is executed, NOT at time of called function is executed\n\nPanic:\n- Occurs when program cannot continue at all\n  - Don't use when file can't be opened, unless it is critical\n  - Use for unrecoverable events - I.E cannot obtain TCP port for web server\n- Function will stop executing\n  - Deferred functions will still fire\n- If nothing handles panic, program will exit\n\nRecover:\n- Used to recover from panics\n- Only useful in deferred functions\n- Current function will not attempt to continue, but higher functions in call stack will<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-default\"\/>\n\n\n\n<p><strong>Pointers<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Creating pointers:\n- Pointer types use an asterisk (*) as a prefix to type pointers to\n  - I.E *int - a point to an integer\n- Use to addressof operator (&amp;) to get the address of a variable\n\nDe-referencing pointers:\n- De-reference a pointer by preceding with an asterisk (*)\n- Complex types (I.E structs) are automatically de-referenced\n\nCreate pointers to objects:\n- Can use the addressof operator (&amp;) if value type already exists\n  - ms := myStruct{foo: 42}\n  - p := &amp;ms\n- Use addressof operator before initializer\n  - &amp;myStruct{foo: 42}\n- Use <strong>new<\/strong> keyword\n  - Can't initialize fields at the same time\n\nTypes with internal pointers:\n- All assignment operations in Go are copy operations\n- Slices and maps contain internal pointers, so copies point to same underlying data<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If-Statements If-Statements: &#8211; Initializers: Allows you to generate a statement to set-up for a comparison &#8211; Variables in an if-statement are block-scoped &#8211; Comparison operators (primarily for numeric types): &#8211; less-than (&lt;) &#8211; greater-than (&gt;) &#8211; less-than-or-equal-to (&lt;=) &#8211; greater-than-or-equal-to (&gt;=) &#8211; double-equals operator (==) &#8211; not-equals operator (!=) &#8211; Logical operators: &#8211; And (&amp;&amp;) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-553","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=553"}],"version-history":[{"count":19,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/553\/revisions"}],"predecessor-version":[{"id":633,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/553\/revisions\/633"}],"wp:attachment":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}