Prerequisites
Before you begin, ensure you have:- Go 1.19 or later installed
- Protocol Buffers compiler (protoc) installed
- Kratos CLI tool installed
Installation
helloworld/
├── api/ # API definitions (protobuf)
│ └── helloworld/
│ └── v1/
├── cmd/ # Application entrypoint
│ └── helloworld/
│ ├── main.go
│ └── wire.go
├── configs/ # Configuration files
│ └── config.yaml
├── internal/ # Private application code
│ ├── biz/ # Business logic
│ ├── conf/ # Configuration structures
│ ├── data/ # Data access layer
│ ├── server/ # Server initialization
│ └── service/ # Service implementation
└── go.mod
# Use a specific template
kratos new myservice --repo https://github.com/go-kratos/kratos-layout.git
# Specify a branch
kratos new myservice --branch main
Project Structure Explained
API Layer (api/)
Contains protobuf definitions for your services:
Business Logic Layer (internal/biz/)
Contains business logic and use cases, independent of data sources.
Data Layer (internal/data/)
Handles data access, database connections, and external service integrations.
Service Layer (internal/service/)
Implements the API contract defined in protobuf files.
Server Layer (internal/server/)
Configures and initializes HTTP and gRPC servers.
Next Steps
Define APIs
Learn how to define APIs using Protocol Buffers
Code Generation
Generate HTTP and gRPC bindings from protobuf
Configuration
Configure your service with multiple sources
Testing
Learn testing strategies for Kratos services
Common Issues
Command not found: kratos
Command not found: kratos
Ensure
$GOPATH/bin is in your PATH:Wire errors during code generation
Wire errors during code generation
Make sure wire is installed:
Port already in use
Port already in use
Change the port in
configs/config.yaml or stop the process using the port.