1Shop cần so phí cho gói 1.200 gram từ TP.HCM ra Hà Nội, nhưng service không nên học cách gọi từng SDK. cheapest(parcel) duyệt danh sách ShippingCarrier (Go: Cheapest(ctx, p, carriers...)) để giữ một hợp đồng chung.The shop needs competing quotes for a 1,200-gram parcel from HCMC to Hanoi, but the service should not learn every SDK. cheapest(parcel) loops over ShippingCarrier (Go: Cheapest(ctx, p, carriers...)) to use one shared contract.
2Lần gọi quote() đầu tiên được thực thi bởi GhtkAdapter vì nó implement ShippingCarrier. Service không cần biết tên method hay kiểu request của SDK GHTK.The first quote() call dispatches to GhtkAdapter because it implements ShippingCarrier. The service needs no knowledge of the GHTK SDK's method names or request types.
3GhtkAdapter tạo FeeRequest mới, chép địa điểm, khối lượng và COD rồi gọi sdk.calculateFee(). SDK nhận đúng định dạng của nó, còn service vẫn chỉ dùng Parcel.GhtkAdapter creates a fresh FeeRequest, maps the locations, weight, and COD, then calls sdk.calculateFee(). The SDK gets its own format while the service keeps using Parcel.
4SDK trả FeeResponse với phí 32.000 VND và deliveryType "standard"; etaDays() đổi nó thành 3 ngày. Nếu SDK ném GhtkException, adapter bọc lại thành CarrierUnavailableException và giữ cause.The SDK returns a FeeResponse with a 32,000 VND fee and deliveryType "standard"; etaDays() turns it into 3 days. If the SDK throws GhtkException, the adapter wraps it in CarrierUnavailableException, keeping the cause.
5Carrier tiếp theo là adapter GHN (bản Go: GHNAdapter). Nó tra districts["HN"] để lấy mã quận của GHN; thiếu ánh xạ thì trả lỗi "ghn: no district mapping" thay vì gọi SDK.The next carrier is the GHN adapter (Go: GHNAdapter). It looks up districts["HN"] to get GHN's district ID; a missing mapping returns "ghn: no district mapping" instead of calling the SDK.
6GHNAdapter gọi client.CalculateFee(ctx, ghn.FeeRequest{ServiceTypeID: 2, ...}) của SDK ghn-go, rồi đổi res.Total thành Quote GHN, ETA 2 ngày. Lỗi SDK được bọc bằng fmt.Errorf("ghn quote: %w").GHNAdapter calls the ghn-go SDK's client.CalculateFee(ctx, ghn.FeeRequest{ServiceTypeID: 2, ...}), then turns res.Total into a GHN Quote with a 2-day ETA. SDK errors are wrapped with fmt.Errorf("ghn quote: %w").
7CarrierFunc là adapter ở mức hàm, giống http.HandlerFunc: ép một func(ctx, p) thường thành Carrier, ví dụ một hãng giả cố định giá trong test, rồi truyền thẳng vào Cheapest.CarrierFunc is a function-level adapter, like http.HandlerFunc: it turns a plain func(ctx, p) into a Carrier, e.g. a fixed-price fake carrier in a test, which can be passed straight into Cheapest.
8Service so feeVnd của các Quote và chọn GHN vì 30.500 nhỏ hơn 32.000 VND. Bản Go bỏ qua carrier lỗi và chỉ trả errors.Join(...) khi mọi carrier đều lỗi. Thêm hãng mới chỉ cần thêm adapter.The service compares the quotes' feeVnd and selects GHN because 30,500 is less than 32,000 VND. The Go version skips failing carriers and returns errors.Join(...) only when all fail. Adding a carrier only needs another adapter.