#Go43 posts
6 min read

Use Cases of Interface-based Functions in Go

The implementation and value of functional interfaces or interface-based functions in Go (golang). What is an interface-based function, and why wrap a function in an interface instead of passing the function directly as a parameter? How does Go's standard library net/http use interface-based functions?

9 min read

Implement an RPC Framework in Go - GeeRPC Day 7 Service Discovery and Registry

A 7-day tutorial on implementing an RPC framework in Go/golang from scratch (7 days implement golang remote procedure call framework from scratch tutorial). Build an RPC framework modeled after the implementation of the golang standard library net/rpc, covering the server, an asynchronous and concurrent client, message encoding and decoding, service registration, and multiple transport protocols including TCP/Unix/HTTP. On Day 7, we implement a simple registry (registry) with capabilities such as timeout-based removal and heartbeat (heartbeat) reception, along with a simple service discovery (server discovery) module.

10 min read

Implement an RPC Framework in Go - GeeRPC Day 6 Load Balancing

A 7-day tutorial on implementing an RPC framework in Go/golang from scratch (7 days implement golang remote procedure call framework from scratch tutorial). Build an RPC framework modeled after the implementation of the golang standard library net/rpc, covering the server, an asynchronous and concurrent client, message encoding and decoding, service registration, and multiple transport protocols including TCP/Unix/HTTP. On Day 6, we implement two simple load balancing (load balance) algorithms: random selection and Round Robin scheduling.

8 min read

Implement an RPC Framework in Go - GeeRPC Day 5 HTTP Protocol Support

A 7-day tutorial on implementing the RPC framework GeeRPC in Go/golang from scratch (7 days implement golang remote procedure call framework from scratch tutorial). Build an RPC framework modeled after the implementation of Go's standard library net/rpc, covering the server, a client supporting asynchronous and concurrent calls, message encoding and decoding, service registration, and multiple transport protocols such as TCP/Unix/HTTP. Day 5 adds support for the HTTP protocol and provides a simple DEBUG page.

6 min read

Implement an RPC Framework in Go - GeeRPC Day 4 Timeout Processing

A 7-day tutorial on implementing the RPC framework GeeRPC in Go/golang from scratch (7 days implement golang remote procedure call framework from scratch tutorial). Build an RPC framework modeled after the implementation of Go's standard library net/rpc, covering the server, a client supporting asynchronous and concurrent calls, message encoding and decoding, service registration, and multiple transport protocols such as TCP/Unix/HTTP. Day 4 provides the RPC framework with the ability to handle timeouts (timeout processing).

10 min read

Implement an RPC Framework in Go - GeeRPC Day 3 Service Registration

A 7-day tutorial on implementing the RPC framework GeeRPC in Go/golang from scratch (7 days implement golang remote procedure call framework from scratch tutorial). Build an RPC framework modeled after the implementation of Go's standard library net/rpc, covering the server, a client supporting asynchronous and concurrent calls, message encoding and decoding, service registration, and multiple transport protocols such as TCP/Unix/HTTP. Day 3 implements service registration, mapping Go structs to services via reflection.

9 min read

Implement an RPC Framework in Go - GeeRPC Day 2 Asynchronous and Concurrent Client

A 7-day tutorial on implementing an RPC framework in Go/golang from scratch (7 days implement golang remote procedure call framework from scratch tutorial). Build an RPC framework modeled after the implementation of the golang standard library net/rpc, covering the server, an asynchronous and concurrent client, message encoding and decoding, service registration, and multiple transport protocols including TCP/Unix/HTTP. On Day 2, we implement a client that supports asynchronous (asynchronous) and concurrent (concurrent) calls.

11 min read

Implement an RPC Framework in Go - GeeRPC Day 1 Server and Message Encoding

A 7-day tutorial on implementing an RPC framework in Go/golang from scratch (7 days implement golang remote procedure call framework from scratch tutorial). Build an RPC framework modeled after the implementation of the golang standard library net/rpc, covering the server, an asynchronous and concurrent client, message encoding and decoding, service registration, and multiple transport protocols including TCP/Unix/HTTP. On Day 1, we implement a simple server along with message encoding and decoding.

4 min read

7 Days to Implement an RPC Framework GeeRPC in Go from Scratch

A 7-day tutorial on implementing an RPC framework in Go/golang from scratch (7 days implement golang remote procedure call framework from scratch tutorial). Build an RPC framework modeled after the implementation of the golang standard library net/rpc, covering the server, an asynchronous and concurrent client, message encoding and decoding, service registration, and multiple transport protocols including TCP/Unix/HTTP. On top of that, it adds features such as protocol exchange, a registry, service discovery, load balancing, and timeout processing.

5 min read

Go Context: A Quick Guide to Concurrency Control

WaitGroup and channels are two common ways to control concurrency, but for complex scenarios Context is a more elegant mechanism. Context provides four control mechanisms: WithCancel, WithValue, WithTimeout and WithDeadline.

6 min read

A Quick Guide to mmap in Go

mmap maps a file or device into memory — a one-to-one mapping between the file's on-disk address and a range of the process's virtual address space. Reading or writing that mapped memory reads or writes the file at the corresponding position. This tutorial shows how to use mmap in Go.

5 min read

Implement an ORM Framework in Go - GeeORM Day 7 Database Migration

A 7-day tutorial on implementing an ORM framework GeeORM in Go/golang from scratch (7 days implement golang object relational mapping framework from scratch tutorial). Build an ORM framework modeled after the implementations of gorm and xorm. When a struct changes, the fields of the database table are migrated automatically; only adding and deleting fields is supported, changing field types is not.

6 min read

Implement an ORM Framework in Go - GeeORM Day 6 Transactions

A 7-day tutorial on implementing an ORM framework GeeORM in Go/golang from scratch (7 days implement golang object relational mapping framework from scratch tutorial). Build an ORM framework modeled after the implementations of gorm and xorm. Introduces transactions in databases; wraps transactions, using user-defined callback functions to implement atomic operations.

4 min read

Implement an ORM Framework in Go - GeeORM Day 5 Implementing Hooks

7 days to implement the ORM framework GeeORM in Go/golang from scratch tutorial (7 days implement golang object relational mapping framework from scratch tutorial), write an ORM framework by hand, modeled after gorm and xorm. Use reflection (reflect) to obtain the hooks (hooks) bound to a struct and invoke them; support calling hooks before and after CRUD (create, read, update, delete) operations.

6 min read

Implement an ORM Framework in Go - GeeORM Day 4 Chain Operations, Update and Delete

7 days to implement the ORM framework GeeORM in Go/golang from scratch tutorial (7 days implement golang object relational mapping framework from scratch tutorial), write an ORM framework by hand, modeled after gorm and xorm. Support stacking query conditions (where, order by, limit, etc.) through chained (chain) operations; implement updating (update), deleting (delete) and counting (count) records.

7 min read

Implement an ORM Framework in Go - GeeORM Day 3 Record Operations

7 days to implement the ORM framework GeeORM in Go/golang from scratch tutorial (7 days implement golang object relational mapping framework from scratch tutorial), write an ORM framework by hand, modeled after gorm and xorm. Implement inserting (insert) records; use reflection (reflect) to convert database records into the corresponding struct instances, implementing the query (select) feature.

9 min read

Implement an ORM Framework in Go - GeeORM Day 2 Mapping Structs to Tables

A tutorial on implementing the ORM framework GeeORM in Go/golang from scratch in 7 days (7 days implement golang object relational mapping framework from scratch tutorial). Build an ORM framework by hand, modeled after gorm and xorm. This article uses reflection (reflect) to obtain the name and fields of an arbitrary struct object and map it to a table in the database; uses dialect to isolate the differences between databases for easy extension; and creates and drops database tables.

10 min read

Implement an ORM Framework in Go - GeeORM Day 1 database/sql Basics

A tutorial on implementing the ORM framework GeeORM in Go/golang from scratch in 7 days (7 days implement golang object relational mapping framework from scratch tutorial). Build an ORM framework by hand, modeled after gorm and xorm. This article introduces basic SQLite operations (connecting to the database, creating tables, inserting and deleting records, etc.) and using the Go standard library database/sql to work with SQLite, including Exec, Query and QueryRow.

5 min read

Implement an ORM Framework in Go from Scratch in 7 Days - GeeORM

A tutorial on implementing the ORM framework GeeORM in Go/golang from scratch in 7 days (7 days implement golang object relational mapping framework from scratch tutorial). Build an ORM framework by hand, modeled after gorm and xorm. Features include two-way mapping between objects and table schemas, creating and dropping tables, CRUD operations on records, transaction support, database migration (migrate), hooks, and more.

4 min read

Implement a Distributed Cache in Go - GeeCache Day 7 Communicating with Protobuf

A tutorial on implementing the distributed cache GeeCache from scratch in Go (7 days implement golang distributed cache from scratch tutorial), building a distributed cache modeled after groupcache. This article describes using protobuf (protocol buffer) for communication between nodes, encoding messages to improve efficiency.

5 min read

Implement a Distributed Cache in Go - GeeCache Day 6 Preventing Cache Breakdown

A tutorial on implementing the distributed cache GeeCache from scratch in Go (7 days implement golang distributed cache from scratch tutorial), building a distributed cache modeled after groupcache. This article introduces the concepts of cache avalanche, cache breakdown and cache penetration, and implements and tests singleflight to prevent cache breakdown.

8 min read

Implement a Distributed Cache in Go - GeeCache Day 5 Distributed Nodes

A tutorial on implementing the distributed cache GeeCache from scratch in Go (7 days implement golang distributed cache from scratch tutorial), building a distributed cache modeled after groupcache. This article adds node registration and node selection to GeeCache, and implements an HTTP client to communicate with the servers of remote nodes.

8 min read

Implement a Distributed Cache in Go - GeeCache Day 4 Consistent Hashing (Hash)

A tutorial on implementing the distributed cache GeeCache from scratch in Go (7 days implement golang distributed cache from scratch tutorial), building a distributed cache modeled after groupcache. This article introduces the principle of consistent hashing, its implementation, and related test cases: why consistent hashing can prevent cache avalanches, and why virtual nodes solve the problem of data skew.

5 min read

A Quick Guide to gomock

gomock is the official Go mocking library for unit tests, useful when dependencies such as network requests, databases or file I/O are complex and hard to call directly. This tutorial covers matchers (Any, Nil, Not, Eq), return values (Do, Return, DoAndReturn), call counts (Times) and ordering (InOrder), and how to write mockable code.

5 min read

Implement a Distributed Cache in Go - GeeCache Day 3 HTTP Server

7 Days Go Distributed Cache Tutorial Series from scratch (7 days implement golang distributed cache from scratch tutorial). Build a distributed cache by hand, modeled after the implementation of groupcache. This article introduces how to build an HTTP Server with the standard library http, set up an HTTP service for GeeCache's single-node instance, and run the related tests.

11 min read

Implement a Distributed Cache in Go - GeeCache Day 2 Single-Node Concurrent Cache

7 Days Go Distributed Cache Tutorial Series from scratch (7 days implement golang distributed cache from scratch tutorial). Build a distributed cache by hand, modeled after the implementation of groupcache. This article introduces the use of the sync.Mutex mutual exclusion lock to control concurrency for the LRU cache. It implements GeeCache's core data structure Group; when the cache misses, a callback function is invoked to fetch the source data.

8 min read

Implement a Distributed Cache in Go - GeeCache Day 1 LRU Cache Eviction Strategy

7 Days Go Distributed Cache Tutorial Series from scratch (7 days implement golang distributed cache from scratch tutorial). Build a distributed cache by hand, modeled after the implementation of groupcache. This article introduces the three most common cache eviction (expiry) algorithms: First In First Out (FIFO), Least Frequently Used (LFU), and Least Recently Used (LRU), and implements the LRU algorithm along with the corresponding test code.

11 min read

Go Test: Unit Testing in Go

How to write unit tests in Go with the standard library testing package: subtests, table-driven tests, helper functions, setup and teardown, network/HTTP testing and benchmarks.

4 min read

Implement a Distributed Cache in Go from Scratch - GeeCache

7 Days Go Distributed Cache Tutorial Series from scratch (7 days implement golang distributed cache from scratch tutorial). Build a distributed cache by hand, modeled after the implementation of groupcache. Features include single-node/distributed caching, LRU (Least Recently Used) cache eviction strategy, preventing cache breakdown, consistent hashing (Consistent Hash), protobuf-based communication, and more.

9 min read

A Quick Guide to Go WebAssembly (Wasm)

Front-end development with Go, WebAssembly and GopherJS: registering functions, interacting with browser JavaScript objects, manipulating DOM elements, asynchronous programming with callbacks, plus more advanced demos (games, rendering) and related projects and documentation.

9 min read

A Quick Guide to Go RPC & TLS

Remote Procedure Call (RPC) in Go with the standard library net/rpc: synchronous and asynchronous calls, plus one-way and mutual authentication between server and client using TLS/SSL.

8 min read

A Quick Guide to Protocol Buffers in Go

Protocol Buffers (protobuf) is a language- and platform-neutral, scalable and extensible way to serialize structured data — smaller and faster than JSON or XML. This tutorial covers Protocol Buffers 3 (proto3): installation, basic syntax and how to use it in Go.

6 min read

Building a Web Framework in Go - Gee Day 7 Panic Recover

A 7-day tutorial on implementing a Web framework from scratch in Go (7 days implement golang web framework from scratch tutorial). Build a Web framework from scratch with Go/golang, with Gin as the design prototype. This article explains how to add an error handling mechanism to the Web framework.

5 min read

Building a Web Framework in Go - Gee Day 6 HTML Template

A 7-day tutorial on implementing a Web framework from scratch in Go (7 days implement golang web framework from scratch tutorial). Build a Web framework from scratch with Go/golang, with Gin as the design prototype. This article explains how to add HTML template (HTML Template) rendering and static file (Serve Static Files) serving to the Web framework.

7 min read

Building a Web Framework in Go - Gee Day 5 Middleware

A 7-day tutorial on implementing a Web framework from scratch in Go (7 days implement golang web framework from scratch tutorial). Build a Web framework from scratch with Go/golang, with Gin as the design prototype. This article explains how to add middleware support (middlewares) to the Web framework.

4 min read

Building a Web Framework in Go - Gee Day 4 Group

A 7-day tutorial on implementing a Web framework from scratch in Go (7 days implement golang web framework from scratch tutorial). Build a Web framework from scratch with Go/golang, with Gin as the design prototype. This article explains the significance of group control (Group Control) and the implementation of nested route groups.

8 min read

Building a Web Framework in Go - Gee Day 3 Trie Tree Router

A tutorial on implementing a Web framework from scratch in Go (7 days implement golang web framework from scratch tutorial), build a Web framework by hand in Go/golang, and design a Web framework from scratch modeled after Gin. This article introduces how to implement routes with a Trie prefix tree. It supports simple parameter parsing and wildcard scenarios.

6 min read

Building a Web Framework in Go - Gee Day 2 Context

A tutorial on implementing a Web framework from scratch in Go (7 days implement golang web framework from scratch tutorial), build a Web framework by hand in Go/golang, and design a Web framework from scratch modeled after Gin. This article introduces the design idea of the request context (Context), which wraps methods for returning JSON/String/Data/HTML and other types of responses.

6 min read

A Quick Guide to Go 2

What changes in Go 2 compared to Go 1: the latest design drafts covering packages, error handling, error values and generics, plus a short history of the Go language.

6 min read

Building a Web Framework in Go - Gee Day 1 http.Handler

A tutorial on implementing a Web framework from scratch in Go (7 days implement golang web framework from scratch tutorial), build a Web framework by hand in Go/golang, and design a Web framework from scratch modeled after Gin. This article introduces the use of Go's standard library net/http and the http.Handler interface, intercepting all HTTP requests and handing them over to the Gee framework.

4 min read

Building a Web Framework in Go from Scratch - Gee Tutorial

A tutorial on implementing a Web framework from scratch in Go (7 days implement golang web framework from scratch tutorial), build a Web framework by hand in Go/golang, and design a Web framework from scratch modeled after Gin.

8 min read

Gin in a Nutshell

A quick-start tutorial for the Gin web framework: installing Go and setting up the environment, hot reload, routing and route groups, HTML templates, and middleware.

18 min read

A Gentle Introduction to Go

Learn Go (Golang) with a single article — installation, basic types (strings, ints, arrays, slices, maps), control flow (if, for, for-range, switch), composite types (structs, interfaces, methods), concurrency (goroutines, channels, sync), error handling (panic, error), Go Modules and how to write unit tests.