{"id":30882055,"url":"https://github.com/matheusaba/golang","last_synced_at":"2026-05-18T04:41:30.959Z","repository":{"id":303677234,"uuid":"1016284760","full_name":"MatheusABA/golang","owner":"MatheusABA","description":"Studying everything about Go","archived":false,"fork":false,"pushed_at":"2025-08-28T20:14:18.000Z","size":15927,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-29T01:58:52.205Z","etag":null,"topics":["channels","gin-gonic","golang","gorm","leetcode-go","pointers"],"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/MatheusABA.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}},"created_at":"2025-07-08T19:13:06.000Z","updated_at":"2025-08-28T20:18:44.000Z","dependencies_parsed_at":"2025-07-08T21:28:40.232Z","dependency_job_id":"29c2cd86-3a40-4b53-af5c-b558f5ecd43b","html_url":"https://github.com/MatheusABA/golang","commit_stats":null,"previous_names":["matheusaba/golang-basics"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/MatheusABA/golang","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatheusABA%2Fgolang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatheusABA%2Fgolang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatheusABA%2Fgolang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatheusABA%2Fgolang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MatheusABA","download_url":"https://codeload.github.com/MatheusABA/golang/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MatheusABA%2Fgolang/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274152754,"owners_count":25231293,"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-09-08T02:00:09.813Z","response_time":121,"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":["channels","gin-gonic","golang","gorm","leetcode-go","pointers"],"created_at":"2025-09-08T08:03:17.267Z","updated_at":"2026-05-18T04:41:30.913Z","avatar_url":"https://github.com/MatheusABA.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Essential Go Commands\n\n| Command                  | Description                                                        |\n|--------------------------|--------------------------------------------------------------------|\n| `go run main.go`         | Runs the Go file directly (compiles and executes).                 |\n| `go build main.go`       | Builds the Go file into an executable.                             |\n| `go mod init \u003cmodule\u003e`   | Initializes a new Go module (creates go.mod).                      |\n| `go mod tidy`            | Cleans up dependencies in go.mod and go.sum.                       |\n| `go get \u003cpackage\u003e`       | Downloads and adds a dependency to your project.                   |\n| `go test`                | Runs all tests in the current directory.                           |\n| `go fmt`                 | Formats your Go source files according to Go standards.            |\n| `go clean`               | Removes object files and cached files.                             |\n| `go doc \u003cpkg or symbol\u003e` | Shows documentation for a package or symbol.                       |\n| `go list`                | Lists packages in your module or workspace.                        |\n| `go env`                 | Displays Go environment variables.                                 |\n| `go version`             | Shows the installed Go version.                                    |\n\n# Leetcode Tracking\n\n[![LeetCode Stats](https://leetcard.jacoblin.cool/matheusaba?ext=activity\u0026border=1)](https://leetcode.com/MatheusABA)\n\n# Annotations\n```go\n// Variables types\n// Primitives\n// int\nvar a = 10       // Infered type int\nvar b int = 20   // Explicit type int\nvar c int64 = 30 // Explicit type int64( size = 2.n^(64-1)) -\u003e -32767 to 32767\n\n// uint -\u003e positive integers only\nvar aa = 10      // Infered type uint\nvar bb uint = 20 // Explicit type uint\n\n// string\nvar d = \"Hello\"        // Infered type string\nvar e string = \"World\" // Explicit type string\n\n// float\nvar floatVar = 3.14         // Infered type float64\nvar floatVar float32 = 2.71 // Explicit type float32(worst precision but less memory)\n\n// alias\nvar alias1 rune = 'A' // rune is an alias for int32\nvar alias2 byte = 'B' // byte is an alias for uint8\n\n// boolean\nvar isTrue = true // Infered type bool\n\n// Another way to declare variables\nvariable := \"This is a variable\" // Short variable declaration with infered type string\n\n\n```\n# Running a file\n### First build to an .exe and then run\n```go\ngo build filename.go\n./filename\n```\n### or just execute\n```bash\ngo run filename.go\n```\n### Example code of a function call with loop/nil error treatment\n```go\npackage main\n\nimport (\n\t\"fmt\"\n)\n\nfunc main() {\n\n\t// Doing integer division\n\tnumerator := 10\n\tdenominator := 0\n\tresult, remainder, error := intDivision(numerator, denominator)\n\t// One way to check conditions is with if-else\n\tif error != nil {\n\t\tfmt.Println(\"Error:\", error)\n\t} else if remainder == 0 {\n\t\tfmt.Printf(\"The result of %d divided by %d is %d\\n\", numerator, denominator, result)\n\t} else {\n\t\tfmt.Printf(\"The result of %d divided by %d is %d and the remainder is %d\\n\", numerator, denominator, result, remainder)\n\t}\n\t// another way to do this is using switch\n\tswitch {\n\tcase error != nil:\n\t\tfmt.Println(\"Error:\", error)\n\tcase remainder == 0:\n\t\tfmt.Printf(\"The result of %d divided by %d is %d\\n\", numerator, denominator, result)\n\tdefault:\n\t\tfmt.Printf(\"The result of %d divided by %d is %d and the remainder is %d\\n\", numerator, denominator, result, remainder)\n\t}\n}\n\nfunc intDivision(numerator int, denominator int) (int, int, error) {\n\tvar error error\n\tif denominator == 0 {\n\t\treturn 0, 0, fmt.Errorf(\"denominator cannot be zero\")\n\t}\n\tresult := numerator / denominator\n\tremainder := numerator % denominator\n\n\t// nil is used to indicate that there is no error during the execution of the program\n\t// if error return nil, it means that there is no error\n\t// return the result of the division, remainder an nil\n\treturn result, remainder, error\n}\n```\n### Arrays, Slices, Maps and Speed Test between traditional and make constructor\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n)\n\nfunc main() {\n\n\t// Array()\n\n\t// Slice()\n\n\t// Map()\n\n\tspeedTest() // Speed test for slice preallocation\n}\n\nfunc Array() {\n\t// The type specify how much memory is allocated for the variable\n\tvar integerArray [10]int16 = [10]int16{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} // Array of integers initialized with a fixed size of 10 [2 bytes each]\n\tintArray := [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}                    // Short way to initialize array of integers initialized with a fixed size of 10 [4 bytes each]\n\tint64Array := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}                  // Short way to initialize array of integers with inferred size [8 bytes each]\n\n\tfmt.Printf(\"Integer Array: %v, Type: %T\\n\", integerArray, integerArray[0])\n\tfor i := range integerArray { // Works exactly as -\u003e for i := 0; i \u003c len(integerArray); i++\n\t\tfmt.Printf(\"Position: %d, Value: %d, Memory Position: %p \\n\", i, integerArray[i], \u0026integerArray[i]) // %p -\u003e pointer\n\t}\n\n\tfmt.Printf(\"Integer Array: %v, Type: %T\\n\", intArray, intArray[0])\n\tfor j := range intArray {\n\t\tfmt.Printf(\"Position: %d, Value: %d, Memory Position: %p \\n\", j, intArray[j], \u0026intArray[j]) // %p -\u003e pointer\n\t}\n\n\tfmt.Printf(\"Integer Array: %v, Type: %T\\n\", int64Array, int64Array[0])\n\tfor k := range int64Array {\n\t\tfmt.Printf(\"Position: %d, Value: %d, Memory Position: %p \\n\", k, int64Array[k], \u0026int64Array[k]) // %p -\u003e pointer\n\t}\n\n}\n\nfunc Slice() {\n\t// Slices are a more flexible way to work with arrays in Go\n\tslice := []int64{1, 2} // Slice of integers initialized with a variable size\n\tfmt.Print(\"--------------- SLICE ---------------\\n\")\n\tfmt.Printf(\"Initial slice %v length %d and capacity %d\\n\", slice, len(slice), cap(slice)) // len() -\u003e length, cap() -\u003e capacity\n\tslice = append(slice, 12, 7, 45)\n\tfmt.Printf(\"Slice after appending values %v and their length %d and capacity %d\\n\", slice, len(slice), cap(slice)) // Appending values to the slice\n\n\tslice2 := []int64{201, 38, 21} // Creating another slice of integers initialized with a variable size to append to the first one\n\n\tslice = append(slice, slice2...)                                                                                          // Appending another slice to the first one with spread operator\n\tfmt.Printf(\"Slice after appending another slice %v and their length %d and capacity %d\\n\", slice, len(slice), cap(slice)) // Printing the final slice\n\n\tfmt.Print(\"--------------- SLICE WITH MAKE ---------------\\n\")\n\tmakeSlice := make([]int64, 5, 10) // Creating a slice with make() function, length of 5 and capacity of 10\n\tfmt.Printf(\"Slice created with make %v and their length %d and capacity %d\\n\", makeSlice, len(makeSlice), cap(makeSlice))\n\tmakeSlice = append(makeSlice, 1, 2, 3, 4, 5)                                                                                            // Appending values to the slice created with make()\n\tfmt.Printf(\"Slice after appending values %v and their length %d and capacity %d\\n\", makeSlice, len(makeSlice), cap(makeSlice))          // Printing the final slice created with make()\n\tmakeSlice = append(makeSlice, slice...)                                                                                                 // Appending the first slice to the slice created with make()\n\tfmt.Printf(\"Slice after appending the first slice %v and their length %d and capacity %d\\n\", makeSlice, len(makeSlice), cap(makeSlice)) // Printing the final slice created with make() after appending the first slice\n}\n\nfunc Map() {\n\t// Maps are a way to store key-value pairs in Go\n\tfmt.Print(\"--------------- MAP ---------------\\n\")\n\t//myMap := make(map[string]uint) // Creating a map with make() function\n\n\tmap2 := map[string]uint{ // Creating another map with key-value pairs\n\t\t\"Matheus\": 25,\n\t\t\"João\":    30,\n\t\t\"Maria\":   28,\n\t\t\"Pedro\":   22,\n\t\t\"Lucas\":   27,\n\t}\n\n\tfmt.Printf(\"ACCESSING MAP VALUE WITH KEY %v\\n\", map2[\"Matheus\"])\n\tfmt.Println(map2[\"Matheus\"]) // Accessing a value in the map with the key -\u003e Map always return value\n\tfmt.Print(\"CHECKING IF KEY EXISTS\\n\")\n\tage, ok := map2[\"Matheus\"] // Accessing a value in the map with the key and checking if it exists\n\tif ok {\n\t\tfmt.Println(age)\n\t} else {\n\t\tfmt.Println(\"Key not found\")\n\t}\n\t// Deleting a key-value pair from the map\n\t// delete(map2, \"Matheus\") // Delete method receive (map, key)\n\t// Iterating over a map\n\tfor key, value := range map2 {\n\t\tfmt.Printf(\"Key: %s, Value: %d\\n\", key, value) // Printing the key and value of the map with random order\n\t}\n}\n\nfunc speedTest() {\n\tn := 1000000                    // Number of iterations for the speed test\n\ttestSlice := []int{}            // Slice without preallocation\n\ttestSlice2 := make([]int, 0, n) // Preallocated slice with capacity n\n\n\tfmt.Printf(\"Total time without preallocation: %v\\n\", timeTrack(testSlice, n))\n\tfmt.Printf(\"Total time with preallocation: %v\\n\", timeTrack(testSlice2, n))\n}\n\nfunc timeTrack(slice []int, n int) time.Duration {\n\tt0 := time.Now()     // Start time tracking\n\tfor len(slice) \u003c n { // Loop until the slice reaches the desired length\n\t\tslice = append(slice, 1) // Appending values to the slice\n\t}\n\treturn time.Since(t0) // Return the elapsed time\n}\n```\n### Strings\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"strings\"\n)\n\nfunc main() {\n\n\tString()\n\n\tconcatenateString()\n}\n\nfunc String() {\n\t// Dealing with strings\n\tmyString := \"Matheus\"\n\tindex := myString[0]\n\tfmt.Printf(\"Hex: %x, Char: %c, Decimal: %d, Octal: %o, Type: %T\\n\", index, index, index, index, index) // Will return the value based on ASCII table\t-\u003e x = hex, c -\u003e char, d -\u003e decimal, %o -\u003e octal\n\tfor k, v := range myString {\n\t\tfmt.Println(k, v)\n\t}\n\n\t// Now with runes -\u003e int32\n\tmyRune := []rune(\"Matheus\")\n\tindexRune := myRune[0]\n\tfmt.Printf(\"Hex: %x, Char: %c, Decimal: %d, Octal: %o, Type: %T\\n\", indexRune, indexRune, indexRune, indexRune, indexRune) // Will return the value based on Unicode table\t-\u003e x = hex, c -\u003e char, d -\u003e decimal, %o -\u003e octal\n\tfor k, v := range myRune {\n\t\tfmt.Println(k, v)\n\t}\n}\n\nfunc concatenateString() {\n\t// Concatenating strings\n\tnewString := []string{\"M\", \"a\", \"t\", \"h\", \"e\", \"u\", \"s\"}\n\tconcatString := \"\"\n\tfor i := range newString {\n\t\tconcatString += newString[i]\n\t}\n\tfmt.Println(\"Concatenated String:\", concatString) // Worst case because it creates a new string every time you concatenate, so it is better to use strings.Builder or bytes.Buffer for large strings\n\n\t//  Using strings.Builder\n\tnewStringBuilder := []string{\"M\", \"a\", \"t\", \"h\", \"e\", \"u\", \"s\"}\n\tstringBuilder := strings.Builder{}\n\tfor i := range newStringBuilder {\n\t\tstringBuilder.WriteString(newStringBuilder[i])\n\t}\n\tstringBuilderString := stringBuilder.String()                         // Only now the string is created\n\tfmt.Println(\"Concatenated String with Builder:\", stringBuilderString) // Better performance for large strings\n\n}\n```\n### Pointers\n```go\n// Pointers are variables that store the memory address of another variable.\n// \u0026 is used to get the address of a variable.\n// * is used to declare a pointer or to dereference (access the value pointed by) a pointer.\n\na := 10\np := \u0026a // p is a pointer to a\n\nfmt.Println(\"Value of a:\", a)      // 10\nfmt.Println(\"Address of a:\", \u0026a)   // address of a\nfmt.Println(\"Value of p:\", p)      // same address as \u0026a\nfmt.Println(\"Value pointed by p:\", *p) // 10\nfmt.Println(\"Address of b:\", \u0026p)   // address of p\n\n// Changing the value of a via pointer\n*p = 20\nfmt.Println(\"New value of a:\", a) // 20\n\n// Pointers with functions\nfunc increment(x *int) {\n    *x = *x + 1\n}\n\nvalue := 5\nincrement(\u0026value)\nfmt.Println(\"Incremented value:\", value) // 6\n\n// Pointers with structs\ntype Person struct {\n    Name string\n}\n\nfunc changeName(p *Person, newName string) {\n    p.Name = newName\n}\n\nperson := Person{Name: \"Maria\"}\nchangeName(\u0026person, \"John\")\nfmt.Println(\"Changed name:\", person.Name) // John\n\n// The zero value of a pointer is nil\nvar ptr *int\nif ptr == nil {\n    fmt.Println(\"ptr is nil\")\n}\n```\n### Generics\n```go\n// Generics allow you to write functions and types that work with any data type.\n// Example: a generic function to swap two values of any type.\n\nfunc Swap[T any](a, b T) (T, T) {\n    return b, a\n}\n\nx, y := Swap[int](1, 2)      // Works with int\ns1, s2 := Swap[string](\"a\", \"b\") // Works with string\n```\n### Structs and Interfaces\n```go\n// Structs are custom types that group fields together.\n\ntype Person struct {\n    Name string\n    Age  int\n}\n\np := Person{Name: \"Alice\", Age: 30}\nfmt.Println(p.Name, p.Age)\n\n\n\n// Interfaces define behavior (methods) that types can implement.\n\ntype Greeter interface {\n    Greet() string\n}\n\ntype Person struct {\n    Name string\n}\n\nfunc (p Person) Greet() string {\n    return \"Hello, \" + p.Name\n}\n\nvar g Greeter = Person{Name: \"Bob\"}\nfmt.Println(g.Greet())\n\n--\n// First we define an interface for handling exceptions\n// To implement this interface, a type must have a Handle method\ntype HandleException interface {\n\t\tHandle() string\n}\n\n// We now implement a struct for the exception that receives\n// a status code and a message\ntype Exception struct {\n\t\tStatus \tint32\n\t\tMessage string\n}\n\n// Then we create a method to handle the exception\n// It needs to satisfy the HandleException interface\n// using Handle method\nfunc (httpException Exception) Handle() string {\n\t\treturn fmt.Sprintf(\"Status: %d, Message: %s\", e.Status, e.Message)\n}\n\n// Now we create a new instance of the Exception struct\n// It creates a new exception with a status code and a message\n// And we assign it to the HandleException interface\n// Because it uses the Handle method\nvar newHandler HandleException = Exception{Status: 200, Message: \"OK\"}\nfmt.Println(newHandler.Handle())\n\n\n```\n### Hot Reload with Air\n[Air](https://github.com/cosmtrek/air) is a popular hot reload tool for Go projects.  \nIt watches your project files and automatically rebuilds and restarts your server whenever you save changes, making development faster and more convenient.\n\nTo install, just run:\n```sh\ngo install github.com/cosmtrek/air@latest\n```\nMake sure your Go binary directory (`$HOME/go/bin` or `%USERPROFILE%\\go\\bin`) is in your system PATH.\nAfter that, you can use Air by running the `air` command in your project directory.\n```sh\nair\n```\nYou can customize Air’s behavior by generating a `.air.toml` configuration file:\n```sh\nair init\n```\nThis file lets you define build commands, specify which files and folders to watch or ignore, and adjust other settings.\n###\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatheusaba%2Fgolang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatheusaba%2Fgolang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatheusaba%2Fgolang/lists"}