GO

Golang: Use viper with local config, management with struct, and envrionment variables

Viper is a complete configuration solution for Go applications. It is designed to work within an application, and can handle all types of configuration needs and formats. Also, viper has many features, such as support for different configuration formats (JSON, TOML, YAML, etc.), live watching and re-reading of config files, reading from environment variables, etc.

Here will demostract how to use viper with local config, management with struct, and envrionment variables.

Continue Reading

Golang Design Pattern Singleton (GORM as example)

Singleton Design Pattern

The Singleton pattern is a creational design pattern that ensures a class has only one instance and provides a global access point to it.

In software development, there are situations where we need to restrict the instantiation of a class to a single object. This is particularly useful when dealing with shared resources or managing global state.

The Singleton pattern is implemented by defining a class with a private constructor and a static method. The first time the static method is called, it creates a new instance of the class. In subsequent calls, it returns the existing instance.

The Singleton pattern provides the following benefits:

  • Ensures that there is only one instance of a class in the application.
  • Provides a global access point to that instance.
  • Delays the initialization of the instance, improving performance and resource utilization.

The following are two implementations of the Singleton pattern in Go, each demonstrating how to connect to a database:

Continue Reading

How to Correctly Update Your Golang Project Version

Before upgrading Go to specific version, you need to upgrade the environment. Next refer to the following sections to upgrade Go version:

Upgrade the Go version in go.mod

The go.mod file specifices the Go version used in project.

Find the current Go version on the file line of go.mod file. For example, if the version is 1.19, change to 1.22 (or whatever version you want to upgrade to). This step is curcial as it tells Go to compile your project with the specified version.

go 1.22

Continue Reading

Asdf Introduction & Quick Guide

asdf is a versatile version manager designed to simplify the management of multiple runtime versions, such as programming languages, tools, and frameworks, within a single development environment. With asdf, users can easily install, manage, and switch between different versions of various software packages, ensuring compatibility and flexibility across projects. Whether you’re a developer, system administrator, or anyone who needs to juggle multiple versions of tools, asdf provides a seamless solution, enhancing productivity and eliminating version-related headaches.

Continue Reading

Deploy a Go Project to Docker hub by Github Action

In this article, we will create a simple golang application CI/CD process and push it to Docker hub by github action. Finally, will illustrate an example of how to use the terraform and docker-compose to deploy a local docker environment.

Continue Reading

Golang: Gin + Gorilla to build a websocket application

Here we used the gorilla module to build a websocket application, and this tutorial will focus on building up a websoccket server. About client part will implement with the chrome extension: Websocket King.

Continue Reading

Golang: 解決 Go Websocket upgrade:websocket: request origin not allowed by Upgrader.CheckOrigin 跨域問題

Golang: 解決 Go Websocket upgrade:websocket: request origin not allowed by Upgrader.CheckOrigin 跨域問題 錯誤訊息: upgrade:websocket: request origin not allowed by Upgrader.CheckOrigin 原因: 如內文所述,在 Go Websocket 判別跨域來源不合法 解決方式: 需要在 websocket.Upgrader 設定 CheckOrigin 來源,例如下方允許所有來源

Continue Reading

Golang - Using Cobra to build your CLI Application

Cobra is a golang library for creating CLI applications and is using by Kubernetes, Hugo, and Github CLI..

Here we will show how to use cobra to build a CLI app.

Continue Reading

Golang - Get Global Environment Variable From Bash Profile (~/.bash_profile)

In golang project, we may need to call os environment variable for security purposes in OSX or Linux.

Here will showing how to set global environment and get value in our golang project.

Continue Reading

Golang Tutorial : Introduction Gin HTML template and how integration with bootstrap

Introduction Gin HTML template and how integration with bootstrap

Gin is going web framework which include server-side render template, we can transfer variable into a template and render by double braces {{ … }}.

In this article, We will introduce how to use gin template, finally will include how to integrate bootstrap with gin HTML template.

Continue Reading

Gin - Middleware (中間件) 程式運作原理及用法教學

Gin - Middleware (中間件) 程式運作原理及用法教學

Gin - Middleware (中間件) 程式運作原理及用法教學

在 Web 應用及開發中,一個完整的請求會包含客戶端請求,伺服器端接收及處理,返回內容給結果。

在真實的場景中,在這些流程還會有更複雜的功能,例如:權限管理,安全驗證及日誌等部分,因此在開發過程我們常要思考,如何更好的管理這些通用的功能,以套件的方式來進行開發,就可以很方便地進行對接及配置。

Continue Reading

GoLang - grpc enum 定義預設類型

GoLang - grpc enum 定義預設類型

在設計 grpc 時,如果我們產品需要分區,不同區域會有不同的產品,該如何處理?

本篇預設讀者對於 grpc 有一定的瞭解,以下將簡單舉例,先說明查單一商品,以及修改為回傳多商品,最後則是 server 與 client 端如何處理這部分的流程:

Continue Reading

GoLang - grpc 使用 Repeated 回傳多筆數據

grpc tutorial 教學

GoLang - grpc 使用 Repeated 回傳多筆數據

在設計 grpc 時,如果我們需要取得商品列表,該如何獲得?

本篇預設讀者對於 grpc 有一定的瞭解,以下將簡單舉例,先說明查單一商品,以及修改為回傳多商品,最後則是 server 與 client 端如何處理這部分的流程:

Continue Reading

Go Module與 Go get 常用參數說明

Golang - 教學 tutorial

Go Module與 Go get 常用參數說明

在 Go 語言結構,可以透過 import

Continue Reading

GoLang - catch panic 避免主系統錯誤

Golang - 教學 tutorial

GoLang - catch panic 避免主系統錯誤

在這幾篇,會以 Go 語言的入門基礎進行逐步說明,本篇針對 catch panic 進行說明

Continue Reading

GoLang - select 監聽多 channel 及設定逾時

Golang - 教學 tutorial

GoLang - select 監聽多 channel 及設定逾時策略

在這幾篇,會以 Go 語言的入門基礎進行逐步說明,本篇針對 多Project Layou 進行說明

Continue Reading

GoLang - 多 goroutine 透過 Channels 協同處理範例

GoLang - 多 goroutine 透過 Channels 協同處理範例

在這幾篇,會以 Go 語言的入門基礎進行逐步說明,本篇針對 多 goroutine 透過 channel 協同處理 進行說明

Continue Reading

GoLang - 多線程全局變數加鎖

Golang - 教學 tutorial

GoLang - 多線程全局變數加鎖

在這幾篇,會以 Go 語言的入門基礎進行逐步說明,本篇針對 多線程全局變數加鎖 進行說明

Continue Reading

GoLang - Channels (上) Buffered / UnBuffered

Golang - 教學 tutorial

GoLang - Channels

在這幾篇,會以 Go 語言的入門基礎進行逐步說明,本篇針對 Channel 進行說明

Continue Reading

GoLang - grpc

GoLang - grpc

在本篇會透過一個簡單的範例,建立一個簡單的 grpc程序, 在開始前,先確認安裝了protocol buffer compiler

接著,開始說明如何建構基本的 grpc 架構:

Continue Reading