{"id":29552722,"url":"https://github.com/theanh130124/api_golang","last_synced_at":"2026-05-17T01:42:05.656Z","repository":{"id":294240164,"uuid":"986166853","full_name":"Theanh130124/API_Golang","owner":"Theanh130124","description":"Go-self-taught","archived":false,"fork":false,"pushed_at":"2025-06-18T05:52:34.000Z","size":753,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-18T06:29:17.370Z","etag":null,"topics":["docker","gin-golang","golang","gorm-orm"],"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/Theanh130124.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-05-19T07:57:51.000Z","updated_at":"2025-06-18T05:55:28.000Z","dependencies_parsed_at":"2025-05-19T14:47:57.011Z","dependency_job_id":"3a5b4fb0-0683-4389-83da-cb08f7907f6a","html_url":"https://github.com/Theanh130124/API_Golang","commit_stats":null,"previous_names":["theanh130124/api_go","theanh130124/api_golang"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Theanh130124/API_Golang","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Theanh130124%2FAPI_Golang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Theanh130124%2FAPI_Golang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Theanh130124%2FAPI_Golang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Theanh130124%2FAPI_Golang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Theanh130124","download_url":"https://codeload.github.com/Theanh130124/API_Golang/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Theanh130124%2FAPI_Golang/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265705565,"owners_count":23814484,"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":["docker","gin-golang","golang","gorm-orm"],"created_at":"2025-07-18T05:35:46.309Z","updated_at":"2026-05-17T01:42:05.609Z","avatar_url":"https://github.com/Theanh130124.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# API_Go\n\n//Có thể sửa port 3308 -\u003e port trong máy mình   (tag mysql là đang lấy bảng mới nhất  -\u003e có thể mysql:8.1.0 ...  -\u003e xem trên dockerhub)\n\ndocker ps\n\ndocker run --name theanh-mysql -e MYSQL_ROOT_PASSWORD=tta1301 -p 3308:3306 -d mysql\n\n\ntruy vấn thì nên đánh index -\u003e trên các cột có where -\u003e hay dùng where để lọc như status \n\n\ndùng explain\nselect  * from ... where status = 'Doing' \n\nGin Framework \n\nThêm vào go.mod \n\n```\n\"github.com/gin-gonic/gin\"\n```\n\nmain.go  -\u003e API đầu tiên\n\n\n``\n  r := gin.Default()\n  r.GET(\"/ping\", func(c *gin.Context) {\n    c.JSON(http.StatusOK, gin.H{\n      \"message\": \"pong\",\n    })\n  })\n  r.Run() // listen and serve on 0.0.0.0:8080 (for windows \"localhost:8080\")\n``\nNote\n```\ntype ListItemStorage interface {\n\tListItem(ctx context.Context,\n\t\tfilter *entity.Filter, // truyen con tro thi co the nil cho truong du lieu nay (con neu khong truyen thi filter luon phai truyen vao)\n\t\tpaging *common.Paging,\n\t\tmoreKey ...string) ([]*entity.TodoItem, error) //Tra ve []TodoItem hoac error\n}\n```\n\n\n\u003ch1\u003eInstall GORM \u003c/h1\u003e\ngo get -u gorm.io/gorm\n\nCài driver với mysql\n\ngo get -u gorm.io/driver/mysql\n\n\n\nClean architecture\n![img.png](img.png)\n\n![img_1.png](img_1.png)\n\n\n\nError chưa custom\n\n![img_2.png](img_2.png)\n\nBug ở đây\n![img_3.png](img_3.png)\n\nCustom lại Error ở đây (Vẫn giữ lại root err)\n\n![img_5.png](img_5.png)\n\nvà bỏ gin.H đi \n![img_6.png](img_6.png)\n\n![img_4.png](img_4.png)\n\n\u003ch2\u003ePanic , Recover , Defer \u003c/h2\u003e\nPanic và Recover (deffered functions -\u003e hàm được khai báo không được chạy ngay(chạy sau cùng))\n(nghĩa là hàm chạy xong rồi thoát ra mới chạy hàm defer)\n-\u003e recover ngược lại với defferered\n![img_7.png](img_7.png)\n\n```\n-panic dừng hàm foo() và chuyển về panicking (nên sẽ thoát ra và khi thoát ra sẽ chạy hàm defer bar()) \n-khi gặp recover lấy lại trạng thái khỏi panicking và chạy tiếp hàm bên foo()\n```\n![img_8.png](img_8.png)\n\n![img_9.png](img_9.png)\n\n\n🧨 1. panic: Gây ra lỗi nghiêm trọng và dừng chương trình\n```\npanic được dùng khi chương trình gặp lỗi không thể xử lý tiếp, ví dụ như chia cho 0, không tìm thấy dữ liệu quan trọng,...\n\nKhi panic được gọi:\n\nLập tức dừng hàm hiện tại.\n\nGọi các defer theo thứ tự LIFO (Last In First Out).\n\nSau đó tiếp tục truyền lỗi lên hàm gọi.\n\nNếu không có recover, chương trình sẽ crash.\n\nVí dụ:\n\nfunc main() {\nfmt.Println(\"Bắt đầu\")\npanic(\"Lỗi nghiêm trọng\")\nfmt.Println(\"Dòng này sẽ không chạy\")\n}\n```\n\n🛡 2. recover: Bắt lại panic để chương trình không bị crash\nrecover chỉ hoạt động bên trong một defer. Nếu có panic, bạn dùng recover() để bắt nó lại, tránh chương trình bị dừng đột ngột.\n```\nVí dụ:\n\nfunc main() {\ndefer func() {\nif r := recover(); r != nil {\nfmt.Println(\"Đã phục hồi sau panic:\", r)\n}\n}()\n\n    fmt.Println(\"Bắt đầu\")\n    panic(\"Lỗi nghiêm trọng\")\n    fmt.Println(\"Dòng này vẫn không chạy, nhưng chương trình không crash\")\n}\n```\n🧹 3. defer: Trì hoãn thực thi hàm đến cuối cùng trước khi thoát hàm\ndefer dùng để đảm bảo tài nguyên được đóng/gọn gàng ngay cả khi có lỗi xảy ra. Các defer sẽ chạy ngược thứ tự khai báo.\n```\nVí dụ:\n\nfunc test() {\ndefer fmt.Println(\"Dọn dẹp 1\")\ndefer fmt.Println(\"Dọn dẹp 2\")\nfmt.Println(\"Thực thi chính\")\n}\n```\nKết quả:\n\n\nThực thi chính\nDọn dẹp 2\nDọn dẹp 1\n\n\nGin midldleware\n![img_10.png](img_10.png)\n\n\ntest\n\nabc","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheanh130124%2Fapi_golang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftheanh130124%2Fapi_golang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheanh130124%2Fapi_golang/lists"}