For a Go JSON PATCH endpoint, keep three inputs distinct when the API needs them: an omitted member means “leave unchanged,” JSON null follows the patch format’s removal or null rule, and a concrete value such as 0 is an update. A plain Go scalar does not record whether its JSON key appeared. Decode presence explicitly before applying changes, and decide the meaning of null from the endpoint’s patch format and contract.
First identify which PATCH format the endpoint accepts
PATCH is an HTTP method, not a single JSON format. The two common formats below give null different meanings, so document the media type and behavior rather than treating them as interchangeable.
| Behavior | JSON Merge Patch (RFC 7396) | JSON Patch (RFC 6902) |
|---|---|---|
| Document shape | An object resembling the target document | An array of operation objects |
| Leave a field unchanged | Omit its member | Include no operation for its path |
| Remove a field | Set the member to null |
Use a remove operation |
| Assign explicit JSON null | Not representable as an ordinary member value: null means removal | Use add or replace with value: null |
| Arrays | Replaced as values; Merge Patch cannot edit part of a non-object target | Operations can address array paths and indices |
| Typical fit | Straightforward object updates without a need to store explicit nulls | Precise operation-level changes or explicit null assignment |
RFC 7396 defines null in a merge patch as removal of an existing value. If a resource must retain an explicit JSON null as distinct from a missing member, JSON Merge Patch cannot express that ordinary member update; JSON Patch can express removal and assignment separately. See the RFC 7396 specification and RFC 6902 specification.
Why a plain Go field loses the distinction
Suppose a request struct has an int field. After decoding, its value is zero both when the request omitted that member and when it supplied "count": 0. The same ambiguity applies to scalar zero values such as false and "". If the endpoint treats supplied values as updates, it must preserve key presence before decoding into a model that only holds values.
#1 Best Overall
- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
A pointer alone is not a universal fix. On a fresh ordinary struct, an absent pointer field and a field explicitly set to JSON null can both decode as nil. A pointer can distinguish a non-null value from nil, but it does not by itself preserve all three states of absent, null, and concrete value.
Decode Merge Patch fields while preserving presence
For an object-shaped Merge Patch request, decode into map[string]json.RawMessage. Map membership tells you whether a key was sent; the raw JSON lets you distinguish null from a concrete value before decoding that value to its Go type.
Rank #2
- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
var patch map[string]json.RawMessage
if err := json.NewDecoder(r.Body).Decode(&patch); err != nil {
// Return a client error for invalid JSON.
}
raw, present := patch["count"]
if !present {
// No requested change to count.
} else if bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
// Apply the API's documented null behavior (or reject null).
} else {
var count int
if err := json.Unmarshal(raw, &count); err != nil {
// Return a client error for a value that is not an integer.
}
// A supplied 0 reaches this branch as a real value.
}
This example shows the decision points, not a complete HTTP handler: production code should handle malformed input, unknown keys, type errors, validation, and authorization explicitly. For each supported field, test map presence first, then handle null according to the contract, then decode and validate a concrete value. Apply only authorized changes to the current resource.
Represent the update decision explicitly
Keep the parsed request separate from the stored resource until you know which changes were requested. For example, a field-level update can be modeled conceptually as a presence flag plus a value, with an additional null state if the API needs one. A wrapper type can carry a Set flag, but the containing request decoder must set it only when the member is present; a value field alone cannot infer that fact.
Rank #3
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
For a larger API, centralize presence and null handling in reusable decoding or patch-application code. Test each important case independently: absent, null, zero, false, and empty string. Also test invalid types and unknown members according to the endpoint’s policy.
Do not use omitempty to detect request presence
omitempty is a marshaling option, not a record of which keys arrived during unmarshaling. In the documented legacy encoding/json behavior, it omits false, numeric zero, nil pointers and interfaces, and empty arrays, slices, maps, and strings when encoding. That can be undesirable when a response must include a meaningful zero or false value, but it does not solve PATCH input tracking. See the Go encoding/json documentation.
Rank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
The same documentation describes omitzero as omitting Go zero values, or values whose IsZero method reports true. JSON v2 documents a different meaning for omitempty: it tests whether the encoded JSON value is empty. These are version- and package-sensitive details; check whether the project uses the legacy package or JSON v2, and which Go version applies, before copying a tag pattern. The JSON v2 documentation describes that package’s behavior.
Build the handler around the contract
- Confirm the wire format. Decide whether the endpoint accepts Merge Patch objects or JSON Patch operation arrays, and document the media type and semantics.
- Preserve input presence. For a Merge Patch object, retain key membership and raw values, such as with
map[string]json.RawMessage. For JSON Patch, parse and validate each operation and its path. - Apply null semantics deliberately. Under Merge Patch, null removes the corresponding object member. Under JSON Patch,
removeremoves a path, whileaddorreplacecan carry a null value. - Decode concrete values by field type. Keep supplied values such as zero, false, and an empty string as actual updates rather than confusing them with omission.
- Validate and authorize changes. Check values and permitted fields before applying updates to the current resource; reject malformed, disallowed, or invalid changes under the API’s defined error policy.
- Test the state distinctions. Cover omitted, null, zero, false, empty string, and invalid-type inputs, plus the removal and explicit-null behavior relevant to the chosen format.
Go’s JSON tutorial documents struct-field encoding and nil-pointer behavior. Those encoding rules do not replace an explicit presence strategy for PATCH requests.
Quick Recap
Best Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

