{
  "slug": "Common-Bug-in-Calculate--Understanding-omitempty-and-Integer-Division-in-Go-d8c5b8da86a1",
  "title": "🧩 Common Bug in Calculate: Understanding omitempty and Integer Division in Go",
  "subtitle": "When building APIs in Go, developers often use the omitempty tag to produce clean JSON outputs. However, fields sometimes mysteriously…",
  "excerpt": "When building APIs in Go, developers often use the omitempty tag to produce clean JSON outputs. However, fields sometimes mysteriously…",
  "date": "2025-07-25",
  "tags": [
    "Go"
  ],
  "readingTime": "1 min",
  "url": "https://medium.com/@mobinshaterian/common-bug-in-calculate-understanding-omitempty-and-integer-division-in-go-d8c5b8da86a1",
  "hero": "https://cdn-images-1.medium.com/max/800/1*iyBii13_o3jD3XVibu80Ww.jpeg",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "🧩 Common Bug in Calculate: Understanding omitempty and Integer Division in Go"
    },
    {
      "type": "paragraph",
      "html": "When building APIs in Go, developers often use the <code>omitempty</code> tag to produce clean JSON outputs. However, fields sometimes mysteriously vanish, particularly computed float values such as percentages. Let’s unpack a classic example that trips up even experienced devs."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*iyBii13_o3jD3XVibu80Ww.jpeg",
      "alt": "🧩 Common Bug in Calculate: Understanding omitempty and Integer Division in Go",
      "caption": "",
      "width": 1024,
      "height": 1024
    },
    {
      "type": "heading",
      "level": 2,
      "text": "🎯 The Scenario"
    },
    {
      "type": "paragraph",
      "html": "You have a price structure like this:"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "type Pricing struct {\n    Price int64 `json:\"price,omitempty\"`\n    PriceOff int64 `json:\"priceOff,omitempty\"`\n    DiscountPercent float32 `json:\"discountPercent,omitempty\"`\n}"
    },
    {
      "type": "paragraph",
      "html": "And a conversion function:"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "func calculate(r *Input) *output {\n    discount := r.discount\n    price := r.productPrice\n    priceOff := price - discount\n    return &output{\n        Price: price, PriceOff: priceOff, DiscountPercent: float32(discount / price),\n    }\n}"
    },
    {
      "type": "paragraph",
      "html": "The result? <code>DiscountPercent</code> mysteriously disappears in your JSON output—even when you expect a non-zero discount."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "🕵️ Root Cause: Integer Division + omitempty"
    },
    {
      "type": "paragraph",
      "html": "Let’s break it down:"
    },
    {
      "type": "list",
      "ordered": false,
      "items": [
        "<code>discount / price</code> is <strong>integer division</strong>, and if <code>discount</code> is smaller than <code>price</code>, the result is <code>0</code>.",
        "Even though you cast the result to<code>float32</code>, it happens <em>after</em> the truncation. So <code>float32(0)</code> is still <code>0.0</code>.",
        "Go’s JSON encoder sees <code>0.0</code> and thanks to <code>omitempty</code>, omits the field."
      ]
    },
    {
      "type": "heading",
      "level": 2,
      "text": "✅ Solution: Embrace Floating Point Precision"
    },
    {
      "type": "paragraph",
      "html": "Fix the division like this:"
    },
    {
      "type": "code",
      "lang": "go",
      "code": "DiscountPercent: float32(discount) / float32(price)"
    },
    {
      "type": "paragraph",
      "html": "This way:"
    },
    {
      "type": "list",
      "ordered": false,
      "items": [
        "You get proper floating-point division.",
        "<code>DiscountPercent</code> reflects accurate values, <code>0.0885</code> instead of vanishing into thin air."
      ]
    },
    {
      "type": "heading",
      "level": 2,
      "text": "🔍 Why This Matters"
    },
    {
      "type": "paragraph",
      "html": "These bugs aren’t flashy, but they undermine your data integrity. Fields that seem “missing” lead to confusion on the frontend and unnecessary debugging time. By paying attention to type behavior and encoding nuances, you maintain a solid and predictable API."
    },
    {
      "type": "paragraph",
      "html": "So next time you see a suspiciously absent field in your JSON, don’t blame the compiler. It’s probably just <code>omitempty</code> quietly doing its job... too well."
    }
  ]
}
