This project is initiated by using command:
go mod init gitlab.com/raihanlh/messenger-api
Use gvm to change between golang version Can be installed from gvm github page
go mod tidy
Install UUID extension on psql
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
Run migration
go run cmd/migration/migration.go
To run all seeder
go run cmd/seeder/seeder.go
To run specific seeder
go run cmd/seeder/seeder.go UserSeed
go run cmd/seeder/seeder.go UserSeed TestSeed
go test -v -coverprofile cover.out ./YOUR_CODE_FOLDER/...
go tool cover -html cover.out -o cover.html
open cover.html
Go has a built-in testing package for both unit and benchmark tests. While unit test is necessary, benchmark test is more of a nice-to-have and is only useful for logic-heavy codes (i.e. usecases, helper functions, etc). Learn more about golang's unit test practices here.
When testing, it's recommended to use an auto-generated mocks so you don't have to manually mock a function everytime you create new one. We're generally using mockgen but feel free to use other tool you're most comfortable using.
- Install mockgen on your computer (not on this project)
go install github.com/golang/mock/[email protected]
- Generate mock
mockgen -source={GOPATH}/example-service/example-folder/example_file.go -destination={GOPATH}/example-service/testing/mocks/example-folder/example_file.go
Example:
mockgen -source=internal/domain/user/user.go -destination=testing/mocks/user/user.go
API Documentation can be accessed through {SERVER_URL}/swagger/index.html
route.
- Add API Operation on new handler function
- Install swaggo on your machine (not on this project)
go install github.com/swaggo/swag/cmd/swag@latest
- Generate docs
swag init -g cmd/server/main.go --output api/docs