Image for post
Image for post

I love CLI applications. They are lightweight, no-dependency, and consume fewer resources. However, building an application for the terminal from scratch is far from easy.

Fortunately, we can always simplify the task with tools or libraries. In this tutorial, I’ll show you how to build a terminal-based dashboard in less than 300 lines of Golang source code by leveraging a great terminal library, termui.

Background

We have a web application that serves millions of users a day. And we want to monitor the real-time memory consumption (and GC) of this application.

You may think about some monitor system and observability platforms like Prometheus + Grafana, but it’s a little bit overkill for this situation. …


Image for post
Image for post

If you visit Rob Pike’s homepage on Twitter, you may notice that his bio contains only one sentence: Ed Is the Standard Text Editor.

There must be something special about Ed as a text editor. This article will explain what makes it so special.

What is Ed

ed is a line editor on Unix systems. When it comes to editors, many people may first think of vi but in fact, ed is its “grandfather”.

ed is kind of a living fossil in the software industry. It was born in 1969, the same age as Unix, and nine years before the C program language was officially released. …


Image for post
Image for post

In the previous story, I generate a static website with Hugo. Today, let me explain how to host this website on IPFS in detail. Hope this story could help you a bit.

What is IPFS

To quote the official website, IPFS is “A peer-to-peer hypermedia. It is designed to make the web faster, safer, and more open.”

It’s unintuitive at first glance, but reiterating its goal should help in understanding this technology.

Its goal is to replace the HTTP protocol and build a distributed and better Web. It defines all the protocols that will be used in the distributed Web. …


Image for post
Image for post

Hugo is an interesting static website generator. It is efficient, concise, and powerful. I’ll show you the pleasant journey of building a website from scratch with it.

Hugo is very easy to learn, so I believe anyone with a basic technical background could quickly create a beautiful static website with Hugo.

Background

Recently, I was learning IPFS and found this stuff to be perfect for hosting static websites since it is content-centric, distributed, tamper-proof, censorship-resistant.

That being said, why not build a Blog on IPFS for fun? …


Finite-State Machine(FSM) is an essential concept of computation. We can find many behaviors of FSM in our real lives such as vending machines, elevators, traffic lights, etc.

FSM-based programming is also a powerful tool to model complicated state transitions, it could significantly simplify our program.

What is a Finite-State Machine

A finite-state machine (FSM) or simply a state machine, is a mathematical model of computation. It is an abstract machine that can be in exactly one of a finite number of states at any given time. …


How to not program when simple CLI tools are enough — using the right tool for the job.

Image for post
Image for post
Photo by Caspar Camille Rubin on Unsplash

Programmers don’t like manual work. They always tend to find the most effective tool to deal with ad hoc jobs.

When there isn’t a tool fit for the job, most of them will turn to write a script (or program) for it. After all, we’re programmers.

But my advice is to consider using old-good CLI tools before writing a script. This is especially true when tackling text-based data files. There’re already many built-in tools in the Unix/Linux ecosystem to manipulate text since Unix has a tradition of preferring text over other data formats.

A suggestion for those programmers who have MS-Windows background only: seriously consider learning Unix even at a basic level, then a new world will be opened to you. …


How to pass structural error information between services in Go Micro v2

Image for post
Image for post
Photo by Sepp Rutz on Unsplash

In a distributed system, it is a common case that an error is produced by one service then returned to another one. It will be great if we could pass all structural errors across services without losing detail information.

The github.com/micro/go-micro/v2/errors package from Micro v2 serves exactly this purpose. Let me quote the introduction from the official documentation:

Go Micro provides abstractions and types for most things that occur in a distributed system including errors. …


Tips and tricks to work with GROM and PostgreSQL

Image for post
Image for post
Photo by Nicolas Thomas on Unsplash.

Unit testing DB-interactive codes is not easy. It becomes harder when an ORM library like GORM is involved.

Theoretically, we could mock all the interfaces of database/sql/driver (such as Conn and Driver) with the great mock tool GoMock. However, we still need a lot of manual work to complete this kind of testing even with the help of GoMock.

The good news is Sqlmock could solve the aforementioned problem. As declared by its official website, it’s an “Sql mock driver for golang to test database interactions.”

This article will show you how to unit test a simple blog application with Sqlmock. The application is backed with PostgreSQL and uses GORM to simplify O-R mapping. …


An opinionated tutorial for unit testing with GoMock

Image for post
Image for post
Photo by Simon Matzinger on Unsplash

Why We Need Mock

Unit testing of codes with dependencies is always a pain. It gets even worse when the dependencies are external resources such as databases, networks, and files.

An intuitive solution to this is to create an isolated environment providing all these external resources, and then run test cases in it. But then they’re not unit tests anymore; they’re integration tests.

Don’t get me wrong. Integration tests have their value and we definitely need them. But they aren’t suitable for running against every code change for three reasons:

  • They are much slower than unit tests.
  • They could fail for many unpredictable reasons.
  • Integration test failure may not directly explain the relationship between the failure and the code change you have made. …

About

Che Dan

programmer / creator

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store