{"id":50555179,"url":"https://github.com/sougata-github/go-learn","last_synced_at":"2026-06-04T06:30:27.404Z","repository":{"id":359349931,"uuid":"1245422485","full_name":"sougata-github/go-learn","owner":"sougata-github","description":"Monorepo with reference code for learning Go. Go Lang ❎ Go Learn ✅","archived":false,"fork":false,"pushed_at":"2026-05-21T12:58:36.000Z","size":1405,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-21T21:32:22.868Z","etag":null,"topics":["api","backend","go","golang","monorepo"],"latest_commit_sha":null,"homepage":"","language":"Go","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/sougata-github.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-05-21T07:53:11.000Z","updated_at":"2026-05-21T12:58:40.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/sougata-github/go-learn","commit_stats":null,"previous_names":["sougata-github/go-learn"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/sougata-github/go-learn","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sougata-github%2Fgo-learn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sougata-github%2Fgo-learn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sougata-github%2Fgo-learn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sougata-github%2Fgo-learn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sougata-github","download_url":"https://codeload.github.com/sougata-github/go-learn/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sougata-github%2Fgo-learn/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33893321,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-04T02:00:06.755Z","response_time":64,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["api","backend","go","golang","monorepo"],"created_at":"2026-06-04T06:30:25.000Z","updated_at":"2026-06-04T06:30:27.394Z","avatar_url":"https://github.com/sougata-github.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GO\n\n## Features\n\n- Go is a **_statically typed_** language (have to declare variable types explicitly or they have to be inferred and these types cannot be changed after declaration)\n\n  here we have to specify the type of the variable when creating it\n\n  ```go\n  var myVariable string = \"Hello, World!\"\n  ```\n\n  or set the value of the type so it can be inferred right away\n\n  ```go\n  var myVariable = \"Hello, World!\"\n  ```\n\n- Go is a **_strongly typed_** language i.e the operation you perform on the variable depends on its type.\n\n  in Go\n\n  ```go\n  a = 1\n  b = \"1\"\n  c = a + b // this will throw an error\n  ```\n\n  is not possible because `a` is an integer and `b` is a string.\n\n  In a **_weakly typed_** language like JavaScript, this would be possible because the type of the variable is not enforced.\n\n  Go being statically and strongly typed language is an advantage since the compiler can do more thorough checking of your code and catch errors at compile time rather than runtime. Also there is better code completion and hinting.\n\n- Go is a **_compiled_** language i.e the code is converted into machine code (binary) before execution that can run as a standalone program (unlike interpreted languages like Python or JavaScript where the code is executed line by line so execution time is slower).\n\n- Compilation time itself is very fast in Go.\n- Go has built in support for concurrency and parallelism (Goroutines and Channels).\n- Simplicity\n\n  This is the general design philosophy of Go. Syntax is pretty concise and we can do a lot with less lines of code. It also has built in **_garbage collection_** which frees up memory when it is no longer needed.\n\n## Setting up Go\n\n- Go to the website [https://go.dev/doc/install](https://go.dev/doc/install) and download the appropriate version for your operating system.\n- Follow the instructions on your computer and wait for the installation to complete.\n- To check if it's installed open your terminal and type `go version` and if the version pop ups that means it's installed.\n\n## Projects in Go\n\n### Modules and Packages\n\nA package is just folder that contains collection of Go files.\n\n    file_1.go\n    file_2.go\n    file_3.go\n\nA collection of these packages is called a module. When we're a intialising a project we're basically initialising a new module.\n\nTo start a new project/module cd into your project directory and then use `go mod init \u003cname-of-your-module\u003e or \u003cgithub-repo-link\u003e`\n\nIt will create a `go.mod` file containing the name of the module and the Go version we're using. If we start installing other external modules, these will be present here along with the version number.\n\nRemember that every Go file is a part of a package. We identify the package to which it belongs to by typing the `package` at the top of the file and then the name of the package. For example:\n\n```go\npackage myPackage\n```\n\nThis has to be same for all the files within this folder.\n\nWe can also use `package main` where `main` is a special package that tells the compiler to look for the entrypoint function here i.e when creating an executable the compiler needs to know where the program should start from and it will look for a function named main within this main package which serves as the first thing that gets executed in the program.\n\nThe `main` package requires the `main` function (this only applies to special package main)\n\n### Functions\n\nTo create a function in Go, use the `func` keyword followed by the name of the function. The main function does not accept any parameters and we use `{}` to signify where the code for the function goes.\n\n```go\npackage main\n\nfunc main(){}\n```\n\nTo print something we use the `fmt` package built into Go. To import a package we go just under the package name declaration and then use `import` to import the required package with the package name i.e `import \"fmt\"`. In Go you have to use the imported packages.\n\n```go\npackage main\nimport \"fmt\"\n\nfunc main(){\n  fmt.Println(\"Hello World!\")\n}\n```\n\n## Running a Go program\n\n- cd into project directory where the `go.mod` file is present\n- compile your program\n\n`go build \u003cfile-location\u003e`\n\n```bash\ngo build package-1/main.go\n```\n\nThis produces a binary file called `main.exe` which we can run to get the output.\n\n```bash\n./main.exe\n```\n\n- we can directly do it using a single command i.e `go run \u003cfile-location\u003e`\n\n```bash\ngo run package-1/main.go\n```\n\n## Variables, Constants and Basic Data Types\n\n### Integers\n\nTo declare a variable in Go use the `var` keyword followed by the variable name and the type.\n\n```go\nvar intNum int\n```\n\nHere the `intNum` is typed to `integer` data type. Similar to imports in Go, variables that have been declared have to be used so that there are no unused variables hanging around in the code.\n\nIn addition to the `int` type there are\n\n- int8\n- int16\n- int32\n- int64\n\nThese represent the memory we use to store our number. 64 can store 4 times larger numbers than 32 but take up more memory. The largest positive number that can be stored in int16 is `32767` — anything larger than this cannot be stored and will throw `overflow` error. However the compiler won't throw any runtime errors, the program will still run but produce weird errors.\n\n```go\nfunc main() {\nvar intNum int16 = 32767\nintNum = intNum+1\nfmt.Println(intNum)\n}\n```\n\nNOTE: `int` will default to 32 bits or 64 bits depending on your system architecture.\n\nIn Go we also have access to unsigned ints i.e `uint` with same sizes as ints but only store positive integer so using `uint` we can store +ve integers twice as large in the same amount of memory.\n\n- uint8\n- uint16\n- uint32\n- uint64\n\nint8: `(-128,127)`\nuint8: `(0,255)`\n\nFor example if we want to store 256 RGB then unsigned 8 bit integer is the best fit rather than 64 bit integer.\n\n### Float\n\nIt is used to store decimal numbers. `float64` can store the largest and most precise decimal numbers but they take more memory.\n\nGo doesn't have just a `float` type, you have to specify the number of bits i.e either `float32` or `float64`\n\nWe can perform arithmetic operations in Go like `+,-,/ and *` but\n\n- you can't perform operations on mixed types, for example adding an `int32` with `float32` or multiply `int64` with `float64` together. If you need to do it, then you would have to type cast the values to common type and then proceed\n\n```go\nvar floatNum32 float32 = 10.1\nvar intNum32 int32 = 2\nvar result = floatNum32 + float(intNum32)\n```\n\n### Strings\n\nWe use a `string` type to store strings. We can initialise strings with `\"\"` or `` ` ` ``.\n\n```go\nvar str1 string = \"Hello World\"\nvar str2 string = `Hello World`\n```\n\nWith backticks you format the string directly without having to use escape characters.\n\n```go\nvar str1 string = \"Hello \\nWorld\"\nvar str2 string = `Hello\nWorld`\n```\n\nWe can concatenate strings by adding them\n\n```go\nvar str1 string = \"Hello\"\nvar str2 string = \"World\"\nvar result = str1 + \" \" + str2\n```\n\nWe can get the length of a string using the built in `len` function but this is not the length of the characters, it's the number of bytes. Since Go uses UTF-8 encoding, characters outside the vanilla ASCII character set are stored with more than a single byte. So for example taking length of ASCII character `A` will result in `1` and taking length of ASCII character `γ` will result in `2`.\n\nFor getting the Unicode code points in Go, import the built in `unicode/utf8` package and call the `RuneCountInString` function.\n\nRunes are also a data type in Go that represents characters unicode. A `rune` is basically an alias for int32.\n\n```go\nfmt.Println(utf8.RuneCountInString(\"γ\"))\n```\n\nTo declare a rune, use the rune type and wrap the value in `''`.\n\n```go\nvar rune1 rune = 'a'\n```\n\n### Booleans\n\nBoolean values can be either `true` of `false`\n\n```go\nvar myBoolean bool = true\n```\n\n### Variable Declaration\n\nUp until now we had always initialised our variable whenever we declared it but this is not required. We could create an int variable and then not set it. In these cases Go sets default values depending on its type.\n\nDefault values for all integers, unsigned integers, floats and runes is `0`. For strings it is empty string i.e `\"\"` and for booleans it is `false`.\n\nWhen we create a variable, we can omit the type and set the variable right away. This way the type is inferred. We could even go a step further and drop the `var` keyword and use the shorthand `:=`.\n\n```go\nvar myVar = 1\n```\n\n```go\nmyVar := \"text\"\n```\n\nWe can initialise multiple variables at once too.\n\n```go\nvar1, var2 := 1,2\n```\n\nFor simple types omitting the types when declaring the variales is fine but adding the type when it isn't obvious (say for a function) is a good practice and will make your code easier to follow.\n\n### Constants\n\nOnce created you cannot change value of a constant. We have to initialise a constant when declared.\n\n```go\nconst pi float32 = 3.14\n```\n\n### Functions \u0026 Control Structures\n\nWe use `func` keyword followed by the name of the function to declare a function.\n\nThe curly braces `{}` defines the logic of the function. The starting curling brace has to be in the first line otherwise there will be an error.\n\n```go\nfunc myFunc(){\n  fmt.printLn(\"Hello World\")\n}\n```\n\nCall your function in the `main` function to execute it.\n\n```go\nfunc main(){\n  myFunc()\n}\n```\n\nWith functions besides the main function we have the ability to pass in parameters which go between the parantheses. Pass the parameter with its name and type.\n\n```go\nfunc myFunc(message string){\nfmt.PrintLn(message)\n}\n```\n\nWe can define a variable in the main function and pass it to the `myFunc` function as the argument when calling it.\n\n```go\nfunc main(){\nvar printMessage string = \"Hello World\"\nmyFunc(printMessage)\n}\n```\n\nWhen a function is returning a value to caller we have to specify the return type of the function.\n\n```go\nfunc main() {\n  var numerator int = 20\n  var denominator int = 5\n  var result int = intDivision(numerator/denominator)\n  fmt.Println(result)\n}\n\nfunc intDivision(numerator int, denominator int) int {\n  var result int = numerator/denominator\n  return result\n}\n```\n\nWhen we want to return multiple values from a function, we can specify multiple values as follows.\n\n```go\nfunc main(){\n  var result, remainder int = intDivision(20,5)\n  fmt.Printf(\"The result of the integer division is %v with remainder %v\", result, remainder)\n}\n\nfunc intDivision (numerator int, denominator int) (int, int) {\n  var result int = numerator/denominator\n  var remainder int = numerator%denominator\n  return result, remainder\n}\n```\n\nNow suppose we pass zero as the denominator then we will get a `integer divide by zero` error. So a design pattern in Go is that if our function can encounter errors then we should have a return type of error along with the result.\n\n`error` is a built in type in Go.\n\n```go\nvar err error\n```\n\nThe default value of an error is `nil`\n\n```go\npackage main\nimport (\n  fmt, errors\n)\n\nfunc main(){\n\n  var result, remainder, err = intDivision (20,5)\n\n  if err!=nil\n  {\n    fmt.Println(err.Error())\n  } else if remainder == 0{\n    fmt.Printf(\"The result of the integer division is %v\", result)\n  }else{\n  fmt.Printf(\"The result of the integer division is %v with remainder %v\", result, remainder)\n  }\n}\n\nfunc intDivision(numerator int, denominator int) (int, int, error) {\n\nvar err error\n\nif denominator == 0\n{\n  err = errors.New(\"Cannot Divide by Zero\")\n  return 0,0, err\n}\n\nvar result int = numerator/denominator\nvar remainder int = numerator%denominator\n\nreturn result, remainder, err\n}\n```\n\nNOTE: You can check equality using `!=` \u0026 `==`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsougata-github%2Fgo-learn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsougata-github%2Fgo-learn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsougata-github%2Fgo-learn/lists"}