{"id":602,"date":"2022-09-02T21:16:41","date_gmt":"2022-09-02T21:16:41","guid":{"rendered":"https:\/\/melfaeforest.xhere.eu.org\/?p=602"},"modified":"2022-09-02T21:16:41","modified_gmt":"2022-09-02T21:16:41","slug":"learning-go-language-day-5","status":"publish","type":"post","link":"https:\/\/melfaeforest.xhere.eu.org\/?p=602","title":{"rendered":"Learning Go Language &#8211; Day 5"},"content":{"rendered":"\n<p><strong>Functions<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Basic syntax:\n- func foo() {\n  ...\n}\n\nParameters:\n- Comma delimited list of variable and types\n  - func foo(bar string, baz int)\n- Parameters of same type list type once, at the end\n  - func foo(bar, baz int)\n- When pointers are passed in, the function can change the value in the caller\n  - This is always true for data of slices and maps\n- Use variadic parameters to send list of same types in\n  - Must be last parameter\n  - Received as a slice\n  - func foo(bar string, baz ...int)\n\nReturn Values:\n- Single return values just list type\n  - func foo() int\n- Multiple return values list types surrounded by parentheses\n  - func foo() (int, error)\n  - The (result type, error) paradigm is a very common idiom\n- Can use named return values\n  - Initializes returned variable\n  - Return using return keyword on its own\n- Can return addresses of local variables\n  - Automatically promoted from local memory (stack) to shared memory (heap)\n\nAnonymous Functions:\n- Functions don't have names if they are:\n  - Immediately Invoked Function Expressions (IIFE), I.E:\n    - func() {\n      ...\n    }()\n  - Assigned to a variable or passed as an argument to a function\n    - a := func() {\n      ...\n    }\n    a()\n\nFunctions as Types:\n- Can assign functions to variables or use as arguments and return values in functions\n- Type signature is like function signature, with no parameter names\n  - var f func(string, string, string) (int, error)\n\nMethods:\n- Function that executes in context of a type\n- Format:\n  - func (g greeter) greet() {\n    ...\n  }\n- Receiver can be value or pointer\n  - Value receiver gets copy of type\n  - Pointer receiver gets pointer to type<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-default\"\/>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Interfaces<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Basic syntax:\ntype Writer interface {\n  Write([]byte) (int, error)\n}\n\ntype ConsoleWriter struct{}\n\nfunc (cw ConsoleWriter) Write(data[]byte) (int, error){\n  n, err := fmt.Println(string(data))\n  return n, err\n}\n\n\n\/\/ Composing Interfaces:\ntype Writer interface {\n  Write([]byte) (int, error)\n}\n\ntype Closer interface {\n  Close() error\n}\n\ntype WriterCloser interface {\n  Writer\n  Closer\n}\n\n\n\/\/ Type Conversion:\nvar wc WriterCloser = NewBufferedWriterCloser()\nbwc := wc.(*BufferedWriterCloser)\n\nImplementing with Values vs. Pointers:\n- Method set of \"value\" is all methods with value receivers\n- Method set of pointer is all methods, regardless of receiver type\n\n\n\/\/ Best Practices:\n- Use many, small interfaces\n  - Single method interfaces are some of the most powerful and flexible Go-lang tools\n    - io.Writer, io.Reader, interface{}\n- Don't export interfaces for types that will be consumed\n- Do export interfaces for types that will be used by packages\n- Design functions and methods to receive interfaces whenever possible<\/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><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Functions Basic syntax: &#8211; func foo() { &#8230; } Parameters: &#8211; Comma delimited list of variable and types &#8211; func foo(bar string, baz int) &#8211; Parameters of same type list type once, at the end &#8211; func foo(bar, baz int) &#8211; When pointers are passed in, the function can change the value in the caller [&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-602","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\/602","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=602"}],"version-history":[{"count":10,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/602\/revisions"}],"predecessor-version":[{"id":625,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=\/wp\/v2\/posts\/602\/revisions\/625"}],"wp:attachment":[{"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/melfaeforest.xhere.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}