Extend grpc methods with custom options in Go
Custom method option in grpc go Recently, I was implementing various apis in Go with gppc/grpc-gateway and a problem arose. Have a look at this example: message GetFlowersRequest { } message GetFlowersResponse { repeated Flower flowers = 1; } message GetMushroomsRequest { } message GetMushroomsResponse { repeated Mushroom mushrooms = 1; } message Flower { string name = 1; string color = 2; } message Mushroom { string name = 1; int64 size = 2; } service GardenService { rpc GetFlowers(GetFlowersRequest) returns (GetFlowersResponse); rpc GetMushrooms(GetMushroomsRequest) returns (GetMushroomsResponse); } In this example, our GardenService has two methods: GetFlowers and GetMushrooms, but not every user can access both methods....
Go: Exploring new loop semantics
Go 1.22 has just been released with a bunch of new features and improvements. In this article, we will explore the new loop semantics and how they can be used to write more expressive and readable code. 1. Loop variable is no longer be shared between iterations Previously, the loop variable was shared between iterations, from go 1.21 (experimental) and now with go 1.22, the loop variable is created anew with each iteration, effectively eliminating one of the most common foot gun in Go (for both experienced and new gophers)....
How to write integration tests with test container in Go
The significance of integration testing in assuring the reliability and seamless operation of software systems cannot be understated. This guide will help you get started with using containers for integration testing and learn how to set up and execute test cases effectively. 1. Use container for testing Let’s consider a small example program where I use postgres as the underlying database to store product records: create table product ( id serial8 not null primary key, name varchar(100) not null, type varchar(50) not null, code varchar(50) not null, price int4 not null ); The program can create, update and list products via a repository:...
Go: Giới thiệu về reflection qua ví dụ
Reflection trong Go Reflection trong go giúp cho ta có thể theo dõi code tại thời điểm runtime, cho phép tiếp cận mã nguồn chương trình dưới dạng data có thể xử lý thay vì các lệnh có thể thực thi (một nhánh trong metaprogramming). Reflection trong go có thể được thực hiện thông qua reflect package. Một số khả năng của reflect: Kiểm tra thông tin của một struct (số lượng method, số lượng field, đọc struct tag…) mà không cần biết trước về struct đó....
Go: Giới thiệu về type parameter (generic) qua ví dụ
1. Kiểm tra sự xuất hiện của một phần tử trong slice Type parameter hay generic là một tính năng mới xuất hiện từ phiên bản 1.18 của Go. Với generic, ta có thể viết các hàm hoặc type có thể dùng được cho nhiều kiểu dữ liệu đầu vào khác nhau mà không cần phải lặp lại code nhiều lần. Hãy bắt đầu với một ví dụ đơn giản và phổ biến: Kiểm tra xem string có xuất hiện trong slice hay không...
Go tips and optimization notes
Some Go tips (maybe some dark side too) and notes for writing better code. 1. Efficiently converse between string and byte slice Gain some performance with the price of maintainability. Using the unsafe package is not advised but we can use it to efficiently converse between a string and byte slice. Use at your own risk. byte slice to string func String(b []byte) (s string) { if len(b) == 0 { return "" } return *(*string)(unsafe....
Create a web framework in Go part 1: static routing
This post contains my notes while implement caneweb - a gin-like minimal web framework/router after reading the first section of 7 days golang by geektutu (https://github.com/geektutu/7days-golang). How standard net/http package handle request? First, let’s look at a sample written with net/http package: func main() { http.HandleFunc("/", handler) http.HandleFunc("/post", getAllPost) log.Fatal(http.ListenAndServe("localhost:8000", nil)) } func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path) } This piece of code binds two endpoints with the corresponding handler function, and starts a web server at port 8000, terminates the server if some errors are returned....
Design pattern thân thiện trong go
Tại sao lại có bài viết này?: Design pattern: Những giải pháp có thể tái sử dụng cho các vấn đề thường gặp tại một ngữ cảnh nhất định trong quá trinh thiết kế phần mềm. Bài viết này nói về một số Design pattern “thân thiện” hơn trong go, được mình tổng hợp dựa trên buổi talk của Ryan Djurovich (https://www.youtube.com/watch?v=HHqv3_rUr88) và một số nguồn tài liệu khác mà mình đọc được....