Practicing Go Project Architecture with Clean Architecture

Over the years, Go has become a widely used programming language across various fields. From foundational components like k8s and Docker to microservices in the business domain, Go can be used to build them all. When constructing these Go projects, the choice of architecture pattern and code layout is subjective. Those with Java Spring experience might opt for the MVC pattern, while those familiar with Python Flask might choose the MTV pattern. Moreover, since there is no mainstream enterprise-level development framework in the Go language domain, many projects lack a clear architectural pattern. ...

December 12, 2021 · 3 min · Zhiya

Avoiding Common Concurrency Bugs in Go

In the paper Understanding Real-World Concurrency Bugs in Go, several researchers analyzed common concurrency bugs in Go and validated them in some of the most popular Go open-source projects. This article organizes the common bugs mentioned in the paper and provides an analysis of solutions. The paper categorizes bugs into two types: blocking and non-blocking: Blocking: A goroutine gets blocked and cannot continue execution (e.g., deadlock). Non-blocking: Execution is not blocked, but there is a potential data race (e.g., concurrent writes). Blocking Bugs Blocking bugs have two root causes: shared memory (e.g., getting stuck on a lock operation intended to protect shared memory) and message passing (e.g., waiting on a channel). The study found that the number of bugs caused by shared memory and message passing is comparable. However, since shared memory is used more frequently than message passing, it was concluded that shared memory is less likely to lead to bugs. ...

August 17, 2021 · 5 min · Zhiya

Exploring the vscode debug process to resolve issues with running Go programs

Problem Description VSCode cannot run Go projects in run mode (it can only debug in debug mode), and the following error occurs. The obscured part in the image is a package within the project, not a third-party package. This means that when running a Go project in run mode, it cannot find other Go files, only the entry file. Initial Investigation The first thought about not finding other files is an issue with the GO_PATH. However, the project uses Go modules, allowing the creation of projects outside the GO_PATH, so this suspicion is ruled out. Next, I suspected an issue with the VSCode configuration. Each VSCode project has a .launch.json file that configures the environment when running code. Below is the .launch.json from the project. ...

April 20, 2020 · 4 min · Zhiya

Issue with Viper Failing to Read Configuration from etcd

Problem Description Viper (version 1.1.0 in this context) is a comprehensive configuration solution for Go applications, widely used across various projects. etcd is a distributed key-value store, often used as a configuration center. Viper supports reading configurations not only from files but also from remote configuration centers. The following code is used for configuration. viper.AddRemoteProvider("etcd", "http://127.0.0.1:2379", "conf.toml") viper.SetConfigType("toml") err := viper.ReadRemoteConfig() if err != nil { panic(err) } Running this results in the error panic: Remote Configurations Error: No Files Found. Upon investigation, it was found that etcd had TLS enabled, requiring access to its API via HTTPS. The code was updated as follows. ...

April 16, 2020 · 3 min · Zhiya

Pitfalls Encountered in Go JSON Practice

During the development process using the Go language, the json package is often used for mutual conversion between JSON and structs. In the process, I encountered some areas that require extra attention, which I have documented below. Integer to Float Conversion Issue Suppose there is a Person structure that contains two fields: Age int64 and Weight float64. Now, let’s convert the Person structure to map[string]interface{} using the json package. The code is as follows: ...

December 24, 2018 · 5 min · Zhiya