{"id":15034291,"url":"https://github.com/webobite/go-lang-learning","last_synced_at":"2026-03-16T19:01:16.543Z","repository":{"id":228117092,"uuid":"773215322","full_name":"webobite/go-Lang-Learning","owner":"webobite","description":"Go Lang Learning resource repo","archived":false,"fork":false,"pushed_at":"2024-03-17T04:36:08.000Z","size":24,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-13T09:44:40.268Z","etag":null,"topics":["golang","golang-examples","learning-resources"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/webobite.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-17T03:32:50.000Z","updated_at":"2024-03-17T04:01:06.000Z","dependencies_parsed_at":"2024-03-28T22:02:13.468Z","dependency_job_id":null,"html_url":"https://github.com/webobite/go-Lang-Learning","commit_stats":null,"previous_names":["webobite/go-lang-learning"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webobite%2Fgo-Lang-Learning","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webobite%2Fgo-Lang-Learning/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webobite%2Fgo-Lang-Learning/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webobite%2Fgo-Lang-Learning/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webobite","download_url":"https://codeload.github.com/webobite/go-Lang-Learning/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243382783,"owners_count":20282007,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","golang-examples","learning-resources"],"created_at":"2024-09-24T20:24:30.524Z","updated_at":"2025-12-24T19:39:36.018Z","avatar_url":"https://github.com/webobite.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# GO Lang - Learning\n\n\u003cimg src=\"https://cdn.worldvectorlogo.com/logos/go-8.svg\" alt=\"Go Logo\" width=\"100\"\u003e\n\n## Go's basic types are\n\n - bool\n- string\n- int  int8  int16  int32  int64\n- uint uint8 uint16 uint32 uint64 uintptr\n- byte // alias for uint8 \n- rune // alias for int32\n    - // represents a Unicode code point\n- float32 float64\n- complex64 complex128\n\nThe example shows variables of several types, and also that variable declarations may be \"factored\" into blocks, as with import statements.\nThe int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.\n\n```\npackage main\n\nimport (\n\t\"fmt\"\n\t\"math/cmplx\"\n)\n\nvar (\n\tToBe   bool       = false\n\tMaxInt uint64     = 1\u003c\u003c64 - 1\n\tz      complex128 = cmplx.Sqrt(-5 + 12i)\n)\n\nfunc main() {\n\tfmt.Printf(\"Type: %T Value: %v\\n\", ToBe, ToBe)\n\tfmt.Printf(\"Type: %T Value: %v\\n\", MaxInt, MaxInt)\n\tfmt.Printf(\"Type: %T Value: %v\\n\", z, z)\n}\n\n```\n**Output :** \nType: bool Value: false\nType: uint64 Value: 18446744073709551615\nType: complex128 Value: (2+3i)\n\n## Zero values\n\nVariables declared without an explicit initial value are given their zero value.\nThe zero value is:\n* 0 for numeric types,\n* false for the boolean type, and\n* \"\" (the empty string) for strings.\n\n## Type conversions\n\nThe expression T(v) converts the value v to the type T.\nSome numeric conversions:\nvar i int = 42\nvar f float64 = float64(i)\nvar u uint = uint(f)\nOr, put more simply:\ni := 42\nf := float64(i)\nu := uint(f)\nUnlike in C, in Go assignment between items of different type requires an explicit conversion.\n\n## Type inference\n\nWhen declaring a variable without specifying an explicit type (either by using the := syntax or var = expression syntax), the variable's type is inferred from the value on the right hand side.\nWhen the right hand side of the declaration is typed, the new variable is of that same type:\nvar i int\nj := i // j is an int\nBut when the right hand side contains an untyped numeric constant, the new variable may be an int, float64, or complex128 depending on the precision of the constant:\ni := 42           // int\nf := 3.142        // float64\ng := 0.867 + 0.5i // complex128\n\n## Constants\n\nConstants are declared like variables, but with the const keyword.\nConstants can be character, string, boolean, or numeric values.\nConstants cannot be declared using the := syntax.\n\n## Numeric Constants\n\nNumeric constants are high-precision values.\nAn untyped constant takes the type needed by its context.\nTry printing needInt(Big) too.\n(An int can store at maximum a 64-bit integer, and sometimes less.)\n\n```\npackage main\n\nimport \"fmt\"\n\nconst (\n\t// Create a huge number by shifting a 1 bit left 100 places.\n\t// In other words, the binary number that is 1 followed by 100 zeroes.\n\tBig = 1 \u003c\u003c 100\n\t// Shift it right again 99 places, so we end up with 1\u003c\u003c1, or 2.\n\tSmall = Big \u003e\u003e 99\n)\n\nfunc needInt(x int) int { return x*10 + 1 }\nfunc needFloat(x float64) float64 {\n\treturn x * 0.1\n}\n\nfunc main() {\n\tfmt.Println(needInt(Small))\n\tfmt.Println(needFloat(Small))\n\tfmt.Println(needFloat(Big))\n\t// fmt.Println(needInt(Big))\n}\n```\n\n## For\n\nGo has only one looping construct, the for loop.\nThe basic for loop has three components separated by semicolons:\n* the init statement: executed before the first iteration\n* the condition expression: evaluated before every iteration\n* the post statement: executed at the end of every iteration\nThe init statement will often be a short variable declaration, and the variables declared there are visible only in the scope of the for statement.\nThe loop will stop iterating once the boolean condition evaluates to false.\nNote: Unlike other languages like C, Java, or JavaScript there are no parentheses surrounding the three components of the for statement and the braces { } are always required.\n\n```\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\tsum := 0\n\tfor i := 0; i \u003c 10; i++ {\n\t\tsum += i\n\t}\n\tfmt.Println(sum)\n}\n```\n\n## For continued\n\nThe init and post statements are optional.\n\n```\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\tsum := 1\n\tfor ; sum \u003c 1000; {\n\t\tsum += sum\n\t}\n\tfmt.Println(sum)\n}\n```\n\n## For is Go's \"while\"\n\nAt that point you can drop the semicolons: C's while is spelled for in Go.\n```\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\tsum := 1\n\tfor sum \u003c 1000 {\n\t\tsum += sum\n\t}\n\tfmt.Println(sum)\n}\n\n```\n\n## Forever\n\nIf you omit the loop condition it loops forever, so an infinite loop is compactly expressed.\n```\npackage main\n\nfunc main() {\n\tfor {\n\t}\n}\n\n```\n\n## If\n\nGo's if statements are like its for loops; the expression need not be surrounded by parentheses ( ) but the braces { } are required.\n\n```\npackage main\n\nimport (\n\t\"fmt\"\n\t\"math\"\n)\n\nfunc sqrt(x float64) string {\n\tif x \u003c 0 {\n\t\treturn sqrt(-x) + \"i\"\n\t}\n\treturn fmt.Sprint(math.Sqrt(x))\n}\n\nfunc main() {\n\tfmt.Println(sqrt(2), sqrt(-4))\n}\n\n```\n\n## If with a short statement\n\nLike for, the if statement can start with a short statement to execute before the condition.\nVariables declared by the statement are only in scope until the end of the if.\n\n```\npackage main\n\nimport (\n\t\"fmt\"\n\t\"math\"\n)\n\nfunc pow(x, n, lim float64) float64 {\n\tif v := math.Pow(x, n); v \u003c lim {\n\t\treturn v\n\t}\n\treturn lim\n}\n\nfunc main() {\n\tfmt.Println(\n\t\tpow(3, 2, 10),\n\t\tpow(3, 3, 20),\n\t)\n}\n```\nOutput :\n9 20\n\n## If and else\n\nVariables declared inside an if short statement are also available inside any of the else blocks.\n(Both calls to pow return their results before the call to fmt.Println in main begins.)\n\n```\npackage main\n\nimport (\n\t\"fmt\"\n\t\"math\"\n)\n\nfunc pow(x, n, lim float64) float64 {\n\tif v := math.Pow(x, n); v \u003c lim {\n\t\treturn v\n\t} else {\n\t\tfmt.Printf(\"%g \u003e= %g\\n\", v, lim)\n\t}\n\t// can't use v here, though\n\treturn lim\n}\n\nfunc main() {\n\tfmt.Println(\n\t\tpow(3, 2, 10),\n\t\tpow(3, 3, 20),\n\t)\n}\n\n```\n\nOutput: \n27 \u003e= 20\n9 20\n\n\n## Switch\n\nA switch statement is a shorter way to write a sequence of if - else statements. It runs the first case whose value is equal to the condition expression.\nGo's switch is like the one in C, C++, Java, JavaScript, and PHP, except that Go only runs the selected case, not all the cases that follow. In effect, the break statement that is needed at the end of each case in those languages is provided automatically in Go. Another important difference is that Go's switch cases need not be constants, and the values involved need not be integers.\n\n```\npackage main\n\nimport (\n\t\"fmt\"\n\t\"runtime\"\n)\n\nfunc main() {\n\tfmt.Print(\"Go runs on \")\n\tswitch os := runtime.GOOS; os {\n\tcase \"darwin\":\n\t\tfmt.Println(\"OS X.\")\n\tcase \"linux\":\n\t\tfmt.Println(\"Linux.\")\n\tdefault:\n\t\t// freebsd, openbsd,\n\t\t// plan9, windows...\n\t\tfmt.Printf(\"%s.\\n\", os)\n\t}\n}\n```\n\n## Switch evaluation order\n\nSwitch cases evaluate cases from top to bottom, stopping when a case succeeds.\n(For example,\nswitch i {\ncase 0:\ncase f():\n}\ndoes not call f if i==0.)\n\n```\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n)\n\nfunc main() {\n\tfmt.Println(\"When's Saturday?\")\n\ttoday := time.Now().Weekday()\n\tswitch time.Saturday {\n\tcase today + 0:\n\t\tfmt.Println(\"Today.\")\n\tcase today + 1:\n\t\tfmt.Println(\"Tomorrow.\")\n\tcase today + 2:\n\t\tfmt.Println(\"In two days.\")\n\tdefault:\n\t\tfmt.Println(\"Too far away.\")\n\t}\n}\n```\n\nSwitch with no condition\nSwitch without a condition is the same as switch true.\nThis construct can be a clean way to write long if-then-else chains.\n\n```\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n)\n\nfunc main() {\n\tt := time.Now()\n\tswitch {\n\tcase t.Hour() \u003c 12:\n\t\tfmt.Println(\"Good morning!\")\n\tcase t.Hour() \u003c 17:\n\t\tfmt.Println(\"Good afternoon.\")\n\tdefault:\n\t\tfmt.Println(\"Good evening.\")\n\t}\n}\n```\n\n## Defer\n\nA defer statement defers the execution of a function until the surrounding function returns.\nThe deferred call's arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.\n\n```\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\tdefer fmt.Println(\"world\")\n\n\tfmt.Println(\"hello\")\n}\n```\nOutput :\n\nworld\n\nhello\n\nLink : https://www.linkedin.com/pulse/golang-best-practices-defer-radhakishan-surwase/\n\n## Stacking defers\n\nDeferred function calls are pushed onto a stack. When a function returns, its deferred calls are executed in last-in-first-out order.\n```\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"counting\")\n\n\tfor i := 0; i \u003c 10; i++ {\n\t\tdefer fmt.Println(i)\n\t}\n\n\tfmt.Println(\"done\")\n}\n\n```\nOutput \n\ncounting\n\ndone\n\n9\n\n8\n\n7\n\n6\n\n5\n\n4\n\n3\n\n2\n\n1\n\n0\n\nLink : https://go.dev/blog/defer-panic-and-recover\n\n\n## Some online resources :\n\n 1. https://go.dev/blog/playground\n 2. https://go.dev/blog/declaration-syntax\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebobite%2Fgo-lang-learning","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebobite%2Fgo-lang-learning","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebobite%2Fgo-lang-learning/lists"}