{"id":15034845,"url":"https://github.com/jensoncs/go-zero-to-hero","last_synced_at":"2025-10-07T13:59:28.387Z","repository":{"id":45078675,"uuid":"145665253","full_name":"jensoncs/go-zero-to-hero","owner":"jensoncs","description":"Golang Learning doc + programs","archived":false,"fork":false,"pushed_at":"2022-09-26T15:09:22.000Z","size":37674,"stargazers_count":26,"open_issues_count":0,"forks_count":12,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-09T23:04:29.235Z","etag":null,"topics":["documentation","golang","golang-examples"],"latest_commit_sha":null,"homepage":null,"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/jensoncs.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}},"created_at":"2018-08-22T06:22:05.000Z","updated_at":"2025-04-01T03:20:50.000Z","dependencies_parsed_at":"2023-01-18T16:45:55.721Z","dependency_job_id":null,"html_url":"https://github.com/jensoncs/go-zero-to-hero","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jensoncs/go-zero-to-hero","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jensoncs%2Fgo-zero-to-hero","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jensoncs%2Fgo-zero-to-hero/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jensoncs%2Fgo-zero-to-hero/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jensoncs%2Fgo-zero-to-hero/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jensoncs","download_url":"https://codeload.github.com/jensoncs/go-zero-to-hero/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jensoncs%2Fgo-zero-to-hero/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278786670,"owners_count":26045588,"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","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"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":["documentation","golang","golang-examples"],"created_at":"2024-09-24T20:26:32.044Z","updated_at":"2025-10-07T13:59:28.358Z","avatar_url":"https://github.com/jensoncs.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"  # Golang Zero To Hero\n\n  Opensource programming language developed by google.\n\n  ## Why Golang\n\n  Golang is a new language and its supports concurrency through creating maximum goroutins(light weight threads).\n  Golang is a statically typed language.\n\n  ## Type of languages\n\n  * Compiled language -: Basically, compiled code can be executed directly by the computer's CPU. That is, the executable code is specified in the CPU's \"native\" language (assembly language).\n     -  Statically typed programming languages do type checking at compile-time (e.g. c, c++, go).\n     -  staically typed will inference the variable while intializing ( eg : var i = true, automatically identify its a boolenan.)\n\n  ## Compile stage\n\n  [Compiler Design - Phases of Compiler](https://www.tutorialspoint.com/compiler_design/compiler_design_phases_of_compiler.htm)\n\n\n\n  * Interpreted languages -: however must be translated at run-time from any format to CPU machine instructions. This translation is done by an interpreter.\n     -  Dynamically typed programming languages do type checking at run-time (e.g. ruby, python).\n\n  # Why Golang?\n\n  * Golang is open-source but backed up by a large corporation\n  * Many big companies used it for their projects including: Google, Uber, Gojek\n  * It’s fast: to learn, compile, deploy and run\n  * modern language\n  * The reason why many developers adopt Golang is its built-in concurrency, which enables you to carry out many processes at the same time. Concurrency is happening via channels and goroutines while garbage collector provides principal support for such execution. Also, in Go concurrency is happening with the static execution speed of C or C++. This is not possible in many back-end languages, in which building process is done in a sequence.\n  * cross platform support\n\n  ## What is multithreading?\n\n  A technique by which a single set of code can be used by several processors at different stages of execution.\n\n  ## Thread v/s process?\n\n  The typical difference is that threads (of the same process) run in a shared memory space, while processes run in separate memory spaces.Threads are an operating environment feature, rather than a CPU feature (though the CPU typically has operations that make threads efficient).\n\n  * An executing instance of a program is called a process.\n  * A thread is a subset of the process.\n\n  ## What is goroutines?\n\n  Goroutines are functions or methods that run concurrently with other functions or methods. Goroutines can be thought of as light weight threads. The cost of creating a Goroutine is tiny when compared to a thread. Hence its common for Go applications to have thousands of Goroutines running concurrently.\n\n  * Goroutines are extremely cheap when compared to threads. They are only a few kb in stack size and the stack can grow and shrink according to needs of the application whereas in the case of threads the stack size has to be specified and is fixed.\n  * The Goroutines are multiplexed to fewer number of OS threads. There might be only one thread in a program with thousands of Goroutines\n  * Goroutines communicate using channels. Channels by design prevent race conditions from happening when accessing shared memory using Goroutines. Channels can be thought of as a pipe using which Goroutines communicate. We will discuss channels in detail in the next tutorial.\n\n  Eg : if you want to execute 100 jobs at a time. cron jobs. If you want to server multiple requests at a time. if you want ot do multiple task at a time.\n\n  ## What is channels?\n\n  Channels are the pipes that connect concurrent goroutines. You can send values into channels from one goroutine and receive those values into another goroutine.\n\n  ## What is race condition?\n\n  A race condition occurs when two or more threads can access shared data and they try to change it at the same time. Because the thread scheduling algorithm can swap between threads at any time, you don't know the order in which the threads will attempt to access the shared data. Therefore, the result of the change in data is dependent on the thread scheduling algorithm, i.e. both threads are \"racing\" to access/change the data.\n\n\n  # Install and start with GO\n\n  ## Mac\n  ```\n  brew install go\n\n  export GOPATH=/User/jenson/go\n\n  GOROOT is for compiler/tools that comes from go installation.\n  GOPATH is for your own go projects / 3rd party libraries (downloaded with \"go get\").\n  ```\n\n  ### Hello world program\n\n  ```\n  package main\n\n  import \"fmt\"\n\n  func main() {\n    fmt.Println(\"Hello World!\")\n  }\n  ```\n\n  # Go-Tour\n\n  ## Packages\n\n  a. Userdefined packages\n  b. systems packges\n\n\n  Every Go program is made up of packages.\n  Programm start running in package main.\n  Used for resuability, readability and maintance\n\n  ```\n  package main\n\n  import \"fmt\"\n  import \"math/rand\"\n\n  func main() {\n    fmt.Println(\"my favourite number is :\", rand.Intn(100))\n  }\n\n  ```\n  ## Imports\n\n  This code groups the imports into a parenthesized, \"factored\" import statement.\n\n  ```\n  import \"fmt\"\n  import \"math\"\n\n  OR\n\n  import (\n    \"fmt\"\n    \"math\"\n  )\n  ```\n\n  ## Naming conventions\n\n  ```\n  file name -: file-name.go\n\n  Exposing variables - Pizza\n\n  local variables - pizza\n\n  E.g. To access exposed variables\n\n  func main() {\n    fmt.Println(math.Pi)\n  }\n  ```\n\n  ## Commands\n\n  ```\n  go run main.go - run\n\n  go build - To build the program\n\n  go clean - to clean all unused files.\n\n  go doc fmt - to see the doc page\n\n  go help - to see available commands with help\n  ```\n  ## Function\n\n  Function can accept zero or more parameters. \n\n  ```\n  package main\n\n  import (\n    \"fmt\"\n  )\n\n  ## Will accept two values (integrer), output also will be as an integer\n\n  func add(x, y int) int {\n    return x + y\n  }\n\n  ## Call add function\n\n  func main() {\n    fmt.Println(add(3, 5))       \n  }\n  ```\n\n  ```\n  package main\n\n  import (\n    \"fmt\"\n  )\n\n  ## Accept two string and output two string\n\n  func reverse(x, y string) (string, string) {\n    return y, x\n  }\n\n  func main() {\n    fmt.Println(reverse(\"jenson\", \"hello\"))\n  }\n\n  ```\n  ## Variables\n\n  The var statement declares a list of variables; as in function argument lists, the type is last.A var statement can be at package or function level.\n\n  ```\n  package main\n\n  import \"fmt\"\n\n  ## declaration boolean variables\n\n  var c, python, java bool\n\n  func main() {\n    ## declaration integrer variable\n\n    var i int\n    fmt.Println(i, c, python, java)\n  }\n  ```\n  Inside a function you can use := for variable declaration\n\n  ```\n  package main\n\n  import \"fmt\"\n\n  func main() {\n    var i, j int = 1, 2\n    k := 3\n    c, python, java := true, false, \"no!\"\n\n    fmt.Println(i, j, k, c, python, java)\n  }\n  ```\n\n  When ever assigning an variable it will save it in memory.\n\n  ## Type inference\n\n  It will inference data types by default.\n\n\n  variable multiple declaration\n  -----------------------------\n  ```\n  var (\n  var i = 10\n  var name = \"jenson\"\n  )\n  ```\n  ## Types\nvatr golang\n  1. bool \n  2. string\n  3.  int8, int16, 1nt32\n\n  *Note*: demo - rune\n\n  ## signed and unsigned integer\n\n\n  polymorphisam\n  ------------\n\n  Many forms \n\n  same function with multiple signature.\n\n  ```\n  add(a,.b) {\n  }\n  add(a,b,c) {\n  }\n  ```\n  static binding - while compile time only it will identify the constanst.\n  ```\n  func add(a,b int) int {     - Function signature\n\n  xxxx                       - Func body\n  xxxx\n  }\n  ```\n  ## Call by reference and call by value\n\n  Call by reference is in func it will refere to the memory address\n\n  func add(\u0026a,\u0026b)\n\n  Call by value is in func values will be passed.\n\n  func add(2,3)\n\n  If any func outputing two and you need only one :- You can use blank identifier(_) to skip any of the output or assign the value to some variable.\n\n  In func You can directly return name is called named return\n\n  ```\n  func add(a,b int) (sum) {\n   sum = a+b\n   return\n  }\n  ```\n  ## Exported names\n\n  Variables starting with capital letters are exported variable in a package.\n\n  Eg: math.Pi\n\n\n  ## Init Function\n\n  func init() {\n\n  }\n  initializing tasks are managed in this section\n\n  ## Control statements\n\n  * If-else\n  ```\n  if condition {\n\n  }else {\n\n  }\n  ```\n  Note: In golang if the else is in second line what will happen? ( By default golang will add ; after all the lines and it will break)\n\n\n  ## Loop\n\n\n  for initialization;condition;increment\n  ```\n  for i=0;i\u003c10;i++ {\n\n  }\n  ```\n  ## Break\n\n  If you want to break the loop\n\n\n  ## Continue\n\n  Check some condition if the condition is passed it will continue to next increment.\n\n\n  ## Notes\n\n  i++ == i =i+1 == i+   =1\n\n  Note: postingrement(i++) and preingrement(++i)\n\n  ## Switch\n\n  To cases\n  ```\n  finger := 2\n\n  switch(finger) {\n\n  case 1:\n    print(\"x\")\n  case 2: \n    print(\"sd\")\n  default:\n   print \"dd\"\n  }\n  ```\n\n  ## Expression less switch\n\n  ```\n  num := 2\n\n  switch {\n\n  case num \u003e 1 \u0026\u0026 num \u003c=4 :\n\n    fmt.println(\"hello\")\n  }\n  ```\n  ## Fallthrough\n\n  If the case is passed and you want to execute next line then you can have fallthrough.\n  ```\n  case 1:\n   print(\"ddd\")\n   fallthrough\n  case 2:\n  ```\n\n  ## Array\n\n  Collection of data\n\n  var a[3] int\n\n  3 - length\n\n  a := [3]int{1,2,3} or a := [...]int{1,2,3}\n\n  ... - will dynamically identify the length\n\n\n  Arry = data type + size (int + 3)\n\n  a := [3]int{1,2,3}\n\n  b := [5]int\n\n  b = a ( will fail because arry type is a combination of size + data type)\n\n\n  Arry in go are value type not refence type\n\n  ## Range :\n\n  it return index and value. using for looping\n\n  ```\n  for _, v := range array {\n                  fmt.Println(v)\n                  sum += v\n          }\n  ```\n\n  ## Slices\n\n  #### Array disadvantages\n\n  * Array extension\n  * merging array\n\n  Slice is very convinient, flexible.\n\n  wrapper over array.\n\n  slice is  a refeerence to the array.\n  ```\n  [1:4] - from 1st position to less than 4\n\n  c := []int{1,2,4}\n  ```\n  length = length of slice\n  capacity = number of elements in the under line array from the index where slice created.\n\n  ```\n  a[...]{1,2,3,4,5,6}\n\n  b := [1:4]\n\n  slice length is 3 {2,3,4}\n  slice capacity is 5 {2,3,4,5,6}\n  ```\n\n  ## Make\n\n  make is a function to create\n\n  make([]int,5-length,5- capacity)\n\n  ## Append\n\n  appending numbers to slice\n\n  ```\n  a := append(a, \"5\")\n  ```\n\n  appaend - func append(s[]T, x....T) [] {}\n\n  ## Memory management\n\n  Garbage collection (mark and sweep in java)\n\n  copy function will help you to copy slice to another slice and you can remove old unsed slice to save memory.\n\n\n  ## Slice Internal\n\n  Slice internally is a struct\n\n  type slice struct {\n\n  length int\n  capacity int\n  zeroth element byte\n  }\n\n  ## Variadic function\n\n  internally(...) will converted as slice.\n\n  In variadic func you can pass slice.\n\n  Eg: \n\n  ```\n  nums := []int{1,2,3,4}\n\n  func find(1,nums...) {\n  }\n  ```\n  **Note**: When your doing slice operation you dont need to output im the function, slice is refering to array and changes will directly happening to array.\n\n ## Slice-Important\n\n You can do slice on string. Because string is made up of runes, runes are made up of bytes, so string is made up of bytes, A string is a buch of bytes; a slice of bytes\n\n## Signed and Unsigned Numbers\n\n`-,+` are signed numbers\n\nonly negative numbers are unsigned numbers.\n\nHow signed and unsiged working\n------------------------------\n\nEg:\n\n0000  1000\n0001  1001\n0002  1002\n\nIf the starting bit we are considing as a signed bit(+,- indicator flag) then the 0001 will be -ve 1 and 10001 will be the +1. If numbers are noted with signed flag called signed numbers. Signed numbers are reducing the capacity by half but its cover lots of other features.\n\nUnsigned numbers are only in positive and it supports more positive values. Eg : 0001 is 1 and 1000 will be 8.\n\n## Map\n\nMap is a key value store.\n\np := make(map[string]int)\n\nstring - Key type\n\nint - value type\n\n\nvalue,ok := map[\"key\"] -  Return the value + the existance of the key\n\nValues out to map while itterating will come in different order.\n\n\n**Delete**\n\nYou can delete keys from map.\n\ndelete(map[\"key\"])\n\nMap is a reference type\n\n**Equality In Map**\n\nMap by default not providing any quality fuction. You can impliment map equality using `range` over each key and copare.\n\n`reflect` - Package is providing map equality check.\n\n\n**Map Implimentation**\n\n* why hashtables\n   * hash tables serach is fast, most widely used data structure.\n   * How hash search is fast?\n    * Example you have a dictionary, you need to find the meaning of `sea` in the noraml array you have to itterate throgh each and every word in the dictionary to get the word `sea`. In hash table hash function will create a pattern and put those pattern into small buckets. Now when your searching for `sea` it knows in which bucket they have to search. They will get it from those bucket very fast instead of itterating through all.\n* map is build upon hashtables\n* How hash tables work? Hash tables are build upon array.\n* Hashfunction function(algoritham) will create hashes for your input and find a common pattern\n* All the common pattern they will be putting into bucket(arry)\n\n\n\n## String\n\n* String in go is slice of bytes.\n* Utf-8 encoded\n* Unicode complained\n\nUsually each character takes `1 byte`, just think about special chartacter if it takes `2 bytes`, it cant print. To solve this problem we have Rune.\n\n* Strings are immutable\n\n## Rune\n\nrune := []rune()\n\n## Pointers \n\ndefault value of pointers is nil.\n\n`\u0026a` = will refer to the memory address of a.\n\n`*a` - Will reference the value of a\n`*` - Reference\n\n## Structs\n\nstructures\n\nuser defined data types\n\ncollection of fields.\n\n```\ntype employee struct {\nname string\nempId int\n}\n```\n\n### Annonymous struct\n\n```\nvar employee struct {\nname string\nempId int\n}\n```\nif your not specifing the type command is called annonymous struct.\n\n### Compare structs \n\nstructs are comparable if the fields are same.\n\neach of their fields are comparable struct variable are not comparable if the struct fields are not comparable.\n\n## Nested struct\n\n```\ntype Person struct {\nname string\naddress address\n}\n\ntype address struct {\ncity,state string\n}\n```\n\n## Methods \n\nMethods are also functions. method is a function with special reciever type that is between func keyword and method name.\n\n```\nfunc (special reciver type) methodName() (outout type) {\n}\n```\n\n## Interface [kind of polymorphisam]\n\n* Why interface\n\nInterface specify what methods a type should have and the type woll decide how to impliment this method.\n\ndefines behaviour of an object.\n\nset of method signature.\n\nE.g : Animal is a interface, animals can walk, eat ...etc\n\nBut tiger is an animal, human is an animal, the walking and eating styleof those animals are different \n\n\nhow to impliment those with interface.\n\n```\ntype Animal interface {\n  eat() string\n  walk() string\n}\n\ntype Tiger struct {\n}\n\ntype Human struct {\n}\n\nfunc (t Tiger) walk() string {\n}\n\nfunc (h Human) walk() string {\n\n}\n\nfunc (t Tiger) eat() string {\n}\n\nfunc (h Human) eat() string {\n\n}\n```\n\n## Concurrency\n\ncontex switching\n\nE.g: if you have a single core processor. the processor will be allocated to one process at a time. it will switch to other process depends. the switching happening in nano seconds.\n\nUser[job1]--doing something[job2]---\u003econtinue[job1]\nat 6.01                6.02             6.03\n\n### parallelism\n\nuser1[job1] -----\u003e user[job2]\n6.00                6.00\n\nGo concurrency is achived throgh goroutines.\n\ngo routines are function or method that run concurrently with other methods or functions. goroutines are light weight threads.\nThe coast of creating goroutines are cheap compare to thread.\n\n### Advantages\n\n* Its cheap compare to thread, they are few KB in size and the stack can grow and shrink according to the need. In threads the stack size is fixed.\n\n* goroutines are multiplexed to fewer no of os threads.\n\ngo routines communicating through channel.\n\nchannels will prevent race condition.\n\nfunc main() - main goroutine.\n\n`go funcName()`\n\n## Channels\n\nChannels are pipes to carry some data. It needs data types.\n\nvar a chan int\n\na := make(chan,int)\n\n\n## Sending and receiving data from channel\n\n```\na := make(chan,int)\n\na \u003c- 1 = Writing integer 1 into the channel\n\ndata := \u003c- a   = Reading data from channel a\n\n```\nSends and recives to a channel is blocking by default, means when data is sent to a channel the control os blocked until some of the goroutine reads from the channel. Similarly when data is read from a channel the data read is blocked until some goroutines write data to the channel.\n\n\n## Deadlock\nIn an operating system, a deadlock occurs when a process or thread enters a waiting state because a requested system resource is held by another waiting process, which in turn is waiting for another resource held by another waiting process.\n\nA deadlock happens when a group of goroutines are waiting for each other and none of them is able to proceed.\n\n```\nfunc main() {\n\tch := make(chan int)\n\tch \u003c- 1\n\tfmt.Println(\u003c-ch)\n}\n```\nThe program will get stuck on the channel send operation waiting forever for someone to read the value. Go is able to detect situations like this at runtime.\n\n## Buffer channel\n\n\n## Waiting group\n\n## Worker pool\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjensoncs%2Fgo-zero-to-hero","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjensoncs%2Fgo-zero-to-hero","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjensoncs%2Fgo-zero-to-hero/lists"}