Skip to content
Breaking
Latest technical intelligence from Northeast India • Infrastructure, AI, Cloud & Security Analysis • Precision Analysis | Raw Intelligence | Your North Star of Tech Latest technical intelligence from Northeast India • Infrastructure, AI, Cloud & Security Analysis • Precision Analysis | Raw Intelligence | Your North Star of Tech
WEBDEV

Analysis: Golang Interfaces for Inheritance: A LINEbot Example

Leveraging Golang Interfaces for Flexible Database Access: A Case Study

Why this matters

In the dynamic world of software development, the ability to adapt to various database systems is crucial for scalability and cost-effectiveness. This article showcases a practical approach to achieving such flexibility using Golang interfaces, demonstrated through a LINE Bot Group Chat Summary Generator example.

Golang Interfaces for Inheritance-like Behavior

By defining interfaces, we can create an abstraction that allows different concrete types to follow a common protocol. This enables us to write data processing logic once and use it with different databases based on the deployment environment variables.

Example: LINE Bot Group Chat Summary Generator

We'll use the LINE Bot Group Chat Summary Generator as an example to illustrate this concept. The code structure is as follows:

Github Repo:

LINE-Bot-ChatSummarizer

Data Structure Diagram

Data Structure Diagram

Implementing Different Database Methods

Related Processing Code

The implementation of `GroupDB` defined as Interfaces is used. Depending on different settings, `NewPGSql(url)` or `NewMemDB()` can make the corresponding implementation inside different.

Basic (Data)

This is the most basic setting. The most important thing is the declaration of the interface `GroupDB`, and then the other two must also have - `ReadGroupInfo(string) GroupData` - `AppendGroupInfo(string, MsgDetail)` The implementation of the two functions, and the input parameters and output parameters must be the same.

Memory DB

This is the implementation of using Memory as the database. You can see that it mainly operates on related data processing through `map`. This way of using memory as a DB, if it is in FAAS (e.g. Heroku or Render.com), you will lose your stored data when the service sleeps.

PostGresSQL DB

It mainly uses the version of the `"github.com/go-pg/pg/v10"` package, which can directly operate PostgresSQL through ORM, which can save a lot of trouble. But in many cases, not using SQL directly is actually more troublesome.

Implications for North East India and India at large

The techniques demonstrated in this article can be beneficial for developers in North East India and across India, as they can help create more adaptable and scalable applications. By using Golang interfaces, developers can reduce the complexity of managing different databases and focus more on the core business logic.

Looking Forward

The approach presented in this article lays the foundation for future development. Whether it's supporting MongoDB or wanting to use MySQL, or even moving the entire database to FireStore, there's no need to change my original business logic. You only need to complete the basic database implementation.