by TritonHo

TritonHo / demo
128 Stars 24 Forks Last release: Not found 14 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


It is a tutorial project to let people understand the backend framework step-by-step.

From phase1 to phase11, you will see know a CRUD endpoint is refactored from 250 lines to only 80 lines. Also, you will learn modern coding skills, such as struct tag(~=Java annotation), reflection, middleware.

這是一個教學用專案,讓你一步一步理解backend framework的基本結構。 從第一版到第十一版,你將會看到一個250行的CRUD endpoint,被改寫成只有80行。 而且,你將會學習到現在編程技巧,例如:struct tag(大約等同Java annotation),reflection和middleware。


| Branch | Description | | -----------|--------| | phase1 | Demonstrate the codes written by novice developer. No attention to code reusability decreases maintainability and productivity. | | phase2 | Introduce of mux library. The routing information is centralized in main.go. | | phase3 | Remove hardcoded database username and password in source code. Introduces dependency injection of database object. | | phase4 | Introduce ORM. | | phase5 | Introduce input binder. Also, demonstrates the power(and pain) of reflection. | | phase6 | Introduce input validator. | | phase7 | Introduce middleware to handle http output. | | phase8 | Introduce database transaction manager. Remove of global database object in handler. | | phase9 | Add user model into the system. Introduce jwt authorization in middleware. | | phase10 | Wrap up repetitive code in CRUD handler | | phase11 | Introduce 3rd party middleware. Make the system become production-ready. |

| 分支 | 說明 | | -----------|--------| | phase1 | 一個菜鳥所寫的程式碼。沒有著重程式重用性,讓程式變得難以維護,也降低人們的生產力。 | | phase2 | 引入mux程式庫。Routing資料現在全集中在main.go。 | | phase3 | 刪掉在程式碼中hardcoded的資料庫用戶名字和密碼。引入資料庫物件的dependency injection。 | | phase4 | 引入ORM. | | phase5 | 引入輸入處理器,也說明reflection的強大(和痛苦)。 | | phase6 | 引入輸入檢查器。 | | phase7 | 引入中間件來負責HTTP輸出。 | | phase8 | 引入資料庫交易管理者。刪掉在handler的全域資料庫物件。 | | phase9 | 加入用戶物件到系統。在中間件層加入jwt身份驗證 | | phase10 | 把重複性程式碼變成獨立程序| | phase11 | 引入第三方套件,讓系統質素到達能上線級別。 |

Getting Started

Mercurial and Git

Some libraries require installation of Mercurial and Git. Please install these yourselves.
If you are using ubuntu, you may run the following command:
(Remarks: Never ask me anything about Windows environment)

sudo apt-get install mercurial git


sudo apt-get install mercurial git

Go 1.5+

The Go packages in ubuntu is outdated. Thus I suggest you downloading the latest Go Compiler yourselves.
In following step, the Go compiler will be stored in your home directory.

  1. Extract the go1.X-linux-amd64.tar.gz file into ~/go-compiler:
    mkdir ~/go-compiler && tar -xvf go1.6.linux-amd64.tar.gz -C ~/go-compiler --strip-components 1
  2. Create the a folder to store all go source code:
    mkdir ~/go
  3. Open ~/.bashrc add following lines in the bottom
    export PATH=$PATH:[homedirectory]/go-compiler/bin
    export GOROOT=[home
    export GOPATH=[home_directory]/go
  4. restart your terminal
  5. Run: go version
    If your compiler installed correctly, you will see something like "go version go1.6 linux/amd64"
  6. cd ~/go/src && git clone

在ubuntu中的Go package是老舊的,我建議你下載最新Go 編譯器

  1. 把下載到的go1.X-linux-amd64.tar.gz解壓縮到 ~/go-compiler:
    mkdir ~/go-compiler && tar -xvf go1.6.linux-amd64.tar.gz -C ~/go-compiler --strip-components 1
  2. 建立資料夾,用來存放所有的原始碼:
    mkdir ~/go
  3. 打開 ~/.bashrc,把以下內容加到最底:
    export PATH=$PATH:[homedirectory]/go-compiler/bin
    export GOROOT=[home
    export GOPATH=[home_directory]/go
  4. 重開你的terminal
  5. 執行: go version
    如果你的Go編譯器正確設定,你應該會看到"go version go1.6 linux/amd64"
  6. cd ~/go/src && git clone

PostgreSQL 9.3+

If you are using ubuntu, I would suggest you simply run: "sudo apt-get install postgresql"
The default installation is enough for development purpose.
The ~/go/src/demo/schema folder contains readme.txt. It will teach you how to create the objects in database.

如果你正在使用ubuntu,我建議你直接執行"sudo apt-get install postgresql"
資料夾 ~/go/src/demo/schema內的readme.txt,會教你怎樣一步一步地建立資料庫內所需物件

Need Help?

If you have a question or feature request, ask me in facebook. GitHub will be used exclusively for bug reports and pull requests.
I also provide backend courses. Please contract me if you want to pay for more knowledge.

如果你有疑問或請求,從facebook找我. GitHub只用作錯誤回報和pull request.


If you think this tutorial is really helpful, I suggest you donate your 2 hours salary to Free Software foundation or Open Culture foundation


We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.