A clean architecture of go project to be used as a template for next development in go.
- Install golang
- Install dep from github.com/golang/dep
go get -u github.com/mokapos/go-standard
cd $GOPATH/src/github.com/mokapos/go-standard
rm -rf .git
- Use this project to init your new project to git.
- Delete this project folder in your local machine.
- DO NOT USE
git clone
, usego get -u github.com/{your git account}/{your new project}
instead! cd $GOPATH/src/github.com/{your git account}/{your new project}
- Enjoy!
General Guidance and Convention
Project Structure
Design Pattern
- Naming Convention
- Comment
- Database
- Error Handling
- Imports
- Project Structure
- Packaging
- Development Environment
For naming convention, golint (https://github.com/golang/lint) will provide the analysis. Then, the code must be cleaned by gofmt and goimports. Last, make sure the source code is already analyzed by govet before committing.
go
provides C-style block comments (/* */
) and line comments (// here lies the comment
). Every package in go
must have a package comments which defined preceeding the package
clause.
/*
Package postgresql will implement Repository interface which will be interact
with database connection to create / read / update / delete data from database.
No business logic will be handled in this level, business logic will be handled
in interactor / usecase level.
*/
package postgresql
type UserRepository struct { ... }
func (ur *UserRepository) GetServices() ([]*Service, error) { ... }
func (ur *UserRepository) GetServiceByID(id uint64) (*Service, error) { ... }
Comments for declaration variable / struct / constant / interface must be defined in full sentence. Comments must begin with the "thing" being described and end with a period. Ex.
// GroupRepository will used as interface to be implemented in repository level.
type GroupRepository interface { ... }
// Lower will set text value to lowercase - will return error when conversion failed.
func Lower(text string) (string, error) { ... }