← Back to articles

GoList

Building a task-list backend with Go and Kratos: service design and development notes.

daily

A local task-list project built with Go-Kratos, with a backend only.

Introduction

A small task-list application for learning and practicing the main Kratos features.

The operations are create, read, update, and delete; the project is primarily an exercise.

Repository:golist。

Initialize

Initialize the project with Kratos and download dependencies.

kratos new golist
go mod tidy 

Define the API and generate code

Use protobuf definitions to generate RPC and HTTP API code.

Annotations describe the mapping between HTTP/JSON and gRPC.

Check the Go version required by the code-generation tools.

Add a new API

Use kratos proto add to create a proto file in the chosen location. The vx directory names indicate API versions; the current example uses v1.

kratos proto add api/golist/v1/todo.proto

The generated proto file is a template rather than an empty file. Adapt it to your own service design.

Proto template

syntax = "proto3";

package api.golist.v1;

option go_package = "golist/api/golist/v1;v1";
option java_multiple_files = true;
option java_package = "api.golist.v1";

service Todo {
    rpc CreateTodo (CreateTodoRequest) returns (CreateTodoReply);
    rpc UpdateTodo (UpdateTodoRequest) returns (UpdateTodoReply);
    rpc DeleteTodo (DeleteTodoRequest) returns (DeleteTodoReply);
    rpc GetTodo (GetTodoRequest) returns (GetTodoReply);
    rpc ListTodo (ListTodoRequest) returns (ListTodoReply);
}

message CreateTodoRequest {}
message CreateTodoReply {}

message UpdateTodoRequest {}
message UpdateTodoReply {}

message DeleteTodoRequest {}
message DeleteTodoReply {}

message GetTodoRequest {}
message GetTodoReply {}

message ListTodoRequest {}
message ListTodoReply {}

Define the todo proto

syntax = "proto3";

package api.golist.v1;

import "google/api/annotations.proto";

option go_package = "golist/api/golist/v1;v1";
option java_multiple_files = true;
option java_package = "api.golist.v1";

service Todo {
    rpc CreateTodo (CreateTodoRequest) returns (CreateTodoReply) {
        option (google.api.http) = {
            post: "/v1/todo",
            body: "*"
        };
    };
    rpc UpdateTodo (UpdateTodoRequest) returns (UpdateTodoReply) {
        option (google.api.http) =  {
            put: "/v1/todo/{id}",
            body: "*"
        };
    };
    rpc DeleteTodo (DeleteTodoRequest) returns (DeleteTodoReply) {
        option (google.api.http) =  {
            delete: "/v1/todo/{id}"
        };
    };
    rpc GetTodo (GetTodoRequest) returns (GetTodoReply) {
        option (google.api.http) =  {
            get: "/v1/todo/{id}",
        };
    };
    rpc ListTodo (ListTodoRequest) returns (ListTodoReply) {
        option (google.api.http) =  {
            get: "/v1/todos",
        };
    };
}

message CreateTodoRequest {
    string title = 1;
}
message CreateTodoReply {
    int64 id = 1;
    string title = 2;
    bool status = 3;
}

message UpdateTodoRequest {
    int64 id = 1;
    string title = 2;
    bool status = 3;
}
message UpdateTodoReply {}

message DeleteTodoRequest {
    int64 id = 1;
}
message DeleteTodoReply {}

message GetTodoRequest {
    int64 id = 1;
}
message GetTodoReply {
    int64 id = 1;
    string title = 2;
    bool status = 3;
}

message ListTodoRequest {

}

message todo {
    int64 id = 1;
    string title = 2;
    bool status = 3;
}

message ListTodoReply {
    repeated todo data = 1;
}

Generate protobuf code

kratos proto client api/golist/v1/todo.proto

Generate server code

kratos proto server api/golist/v1/todo.proto -t internal/service

Adjust the project structure

The initial Kratos template contains example code that differs from our business logic. Replace its helloworld example with our own service.

The following diagram gives a detailed overview of the Kratos project structure.

daily

Implement the features

The implementation is in golist。

create

daily

daily

get

daily

daily

getAll

daily

update

daily

daily

daily

delete

daily

References

Qimi’s course

Kratos

Kratos documentation

About this article’s date

The old date, 2025-01-11, conflicts with publication history. This entry uses 2026-02-13, the earliest verifiable publication on the old site. The writing date is unconfirmed.