{"id":537,"date":"2022-08-30T21:25:20","date_gmt":"2022-08-30T21:25:20","guid":{"rendered":"https:\/\/melfaeforest.xhere.eu.org\/?p=537"},"modified":"2022-09-06T18:41:02","modified_gmt":"2022-09-06T18:41:02","slug":"learning-go-language-day-2","status":"publish","type":"post","link":"https:\/\/melfaeforest.xhere.eu.org\/?p=537","title":{"rendered":"Learning Go Language &#8211; Day 2"},"content":{"rendered":"\n<p><strong>Primitives<\/strong><\/p>\n\n\n\n<p><strong>Booleans<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Booleans:\n- Values are true or false\n- NOT an alias for other types (I.E int) unlike in other programming languages\n- Zero value is false\n\n\n\/\/ Boolean example\nfunc main() {\n  n := 1 == 1\n  m := 1 == 2\n  fmt.Printf(\"%v, %T\\n\", n, n)\n  fmt.Printf(\"%v, %T\\n\", m, m)\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Numeric Types<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Numeric Types:\n- Signed Integers:\n  - int types have varying sizes, but it is always a minimum of 32 bits\n  - 8 bit (int8) through 64 bit (int64)\n- Unsigned integers:\n  - 8 bit (byte and uint8) through 32 bit (uint32)\n\n- Arithmetic operations:\n  - Addition, subtraction, multiplication, division, remainder\n\n\n\/\/ Arithmetic example\nfunc main() {\n  a := 10\n  b := 3\n  fmt.Printf(a + b)\n  fmt.Printf(a - b)\n  fmt.Printf(a * b)\n  fmt.Printf(a \/ b)\n  fmt.Printf(a % b)\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Bitwise Operations<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Bitwise Operation Types:\n - And, or, xor, and not\n\n\n\/\/ Bitwise operations example\nfunc main() {\n  a := 10 \/\/ 1010\n  b := 3  \/\/ 0011\n\n  \/\/\/\/ And operator\n  \/\/ Bits are set if both numbers has that bit\n  fmt.Printf(a &amp; b)  \/\/ 0010 = 2\n\n  \/\/\/\/ Or operator\n  \/\/ Bits are set if they are in EITHER number\n  fmt.Printf(a | b)  \/\/ 1011 = 11\n\n  \/\/\/\/ Xor operator\n  \/\/ Bits are set if one number has it, BUT NOT both\n  fmt.Printf(a ^ b)  \/\/ 1001 = 9\n\n  \/\/\/\/ And Not operator\n  \/\/ Bits are set if neither one of the numbers has that bit\n  fmt.Printf(a &amp;^ b) \/\/ 0100 = 8\n}\n\n\/\/ BONUS: Bitshifting example\nfunc main() {\n  a := 10\n\n  \/\/ Shifting to the left\n  fmt.Printf(a &lt;&lt; 3) \/\/ 2^3 * 2^3 = 64\n\n  \/\/ Shifting to the right\n  fmt.Printf(a &gt;&gt; 3) \/\/ 2^3 \/ 2^3 = 2^0\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Floating Point Numbers<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Floating Point Numbers:\n- Follows IEEE-754 standard\n- Zero value is 0\n- 32 and 64 bit versions\n- Literal styles:\n  - Decimal (3.14)\n  - Exponential (13e18 or 2E10)\n  - Mixed (13.7e12)\n- Arithmetic operations:\n  - Addition, subtraction, multiplication, division\n\n\n\/\/ Floating point number example\n\/\/ NOTE: These variables will be initialized as float64\nfunc main() {\n  n := 3.14\n  n = 13.7e72\n  n = 2.1E14\n\n  fmt.Printf(\"%v, %T\", n, n)\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Strings<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Text Types:\n- Strings:\n  - UTF-8\n  - Immutable\n  - Can be concatenated with plus (+) operator\n  - Can be converted to []byte\n\n- Rune:\n  - UTF-32\n  - Alias for int32\n  - Special methods normally required to process\n    - I.E strings.Reader#ReadRune\n\n\n\/\/ String example\nfunc main() {\n  s := \"this is a string\"\n\n  fmt.Printf(\"%v, %T\\n\", string(s[2]), s)\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>Consts &amp; Iota<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Constants:\n- Immutable, but can be shadowed\n- Replaced by the compiler at compile time\n  - Value must be calculable at compile time\n- Named like variables:\n  - PascalCase for exported constants\n  - camelCase for internal constants\n- Typed constants work like immutable variables\n  - Can interoperate only with same type\n- Untyped constants work like literals\n  - Can interoperate with similar types\n\n\nfunc main() {\n  const myConst int = 42\n  fmt.Printf(\"%v, %T\\n\", myConst, myConst)\n\n  \/\/ Const variables can hold all the primitive times\n  const a int = 14\n  const b string = \"foo\"\n  const c float32 = 3.14\n  const d bool = true\n  fmt.Printf(\"%v\\n\", a)\n  fmt.Printf(\"%v\\n\", b)\n  fmt.Printf(\"%v\\n\", c)\n  fmt.Printf(\"%v\\n\", d)\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Enumerated Constants:\n- Special symbol iota allows related constants to be created easily\n- Iota starts at 0 in each const block and increments by one\n  - After the first instance of Iota in a const block, you do not need the other variable declarations to say iota\n- Watch out for constant values that match zero values for variables\n\nEnumerated Expressions:\n- Operations that can be determined at compilation time are allowed:\n  - Arithmetic\n  - Bitwise operations\n  - Bitshifting<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/\/\/ Basic Iota example\n\/\/ You can put a iota in a const block too\nconst (\n  a = iota\n  b\n  c\n)\n\nconst (\n  a2 = iota\n)\n\nfunc main() {\n\/\/ Will print out 0, 1, 2, each on new lines.\n  fmt.Printf(\"%v\\n\", a)\n  fmt.Printf(\"%v\\n\", b)\n  fmt.Printf(\"%v\\n\", c)\n  \n  \/\/ A seperate iota instance\n  fmt.Printf(\"%v\\n\", a2)\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/\/\/ Iota Example #1: Basic specialist\nconst (\n  \/\/ \"_\" just means \"this is a throwaway, don't use this variable\"\n  _ = iota\n  catSpecialist\n  dogSpecialist\n  snakeSpecialist\n)\n\nfunc main() {\n  var specialistType int = catSpecialist\n  fmt.Printf(\"%v\\n\", specialistType == catSpecialist)\n}<\/pre>\n\n\n\n<p>ae<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/\/\/ Iota Example #2: File size calculus\nconst (\n  _ = iota\n  KB = 1 &lt;&lt; (10 * iota)\n  MB\n  GB\n  TB\n  PB\n  EB\n  ZB\n  YB\n)\n\nfunc main() {\n  fileSize := 4000000000.\n  fmt.Printf(\"%.2fGB\\n\", fileSize\/GB)\n}<\/pre>\n\n\n\n<p>ae<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/\/\/ Iota Example #3: Fancy manager key access\nconst (\n  isAdmin = 1 &lt;&lt; iota\n  isHeadquarters\n  canSeeFinancials\n\n  canSeeAfrica\n  canSeeAsia\n  canSeeEurope\n  canSeeNorthAmerica\n  canSeeSouthAmerica\n)\n\nfunc main() {\n  var roles byte = isAdmin | canSeeFinancials | canSeeEurope\n  fmt.Printf(\"%b\\n\", roles)\n  fmt.Printf(\"Is Admin? %v\\n\", isAdmin &amp; roles == isAdmin)\n  fmt.Printf(\"Is HQ? %v\\n\", isHeadquarters &amp; roles == isHeadquarters)\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Arrays &amp; Slices<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Arrays:\n- Collection of items with same type (no mixing types)\n- Fixed size\n- Declaration styles:\n  - a := [3]int{1, 2, 3}\n  - a := [...]int{1, 2, 3}\n  - var a [3]int\n- Accessed via zero-based index\n  - a := [3]int{1, 3, 5} \/\/ a[1] == 3\n- len function returns size of array\n- Copies refer to different underlying data\n\nSlices:\n- Backed by array\n- Creation styles:\n- Slice existing array or slice\n- Literal style\n- Via <strong>make<\/strong> function\n  - a := make([]int, 10) \/\/ Creates slice with length and capacity == 10\n  - a := make([]it, 10, 100) \/\/ Creates slice with length == 10 and capacity == 100\n- \"len\" function returns length of slice\n- \"cap\" function returns length of underlying capacity\t\n- Use <strong>append<\/strong> function to add elements to slices\n  - May cause expensive copy operation if underlying array is too small\n- Copies refer to same underlying array<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Primitives Booleans Booleans: &#8211; Values are true or false &#8211; NOT an alias for other types (I.E int) unlike in other programming languages &#8211; Zero value is false \/\/ Boolean example func main() { n := 1 == 1 m := 1 == 2 fmt.Printf(&#8220;%v, %T\\n&#8221;, n, n) fmt.Printf(&#8220;%v, %T\\n&#8221;, m, m) } Numeric Types [&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-537","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\/537","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=537"}],"version-history":[{"count":28,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/537\/revisions"}],"predecessor-version":[{"id":634,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/537\/revisions\/634"}],"wp:attachment":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}