# I need my nulls

**URL:** <https://forum.golangbridge.org/t/i-need-my-nulls/19097>\
**Category:** Code Review\
**Created:** [May 26, 2020, 12:14pm UTC](https://forum.golangbridge.org/t/i-need-my-nulls/19097 "2020-05-26T12:14:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sivan](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/sivan/32/7246_2.png) [@sivan](https://forum.golangbridge.org/u/sivan)\
**Post date:** [May 26, 2020, 12:14pm UTC](https://forum.golangbridge.org/t/i-need-my-nulls/19097/1 "2020-05-26T12:14:11Z")

</div>

Hello,  
I’m looking for a way to retain null values sent from a REST API server in JSON. Currently, I’m using a struct and unmarshal but this turns Nulls into zeros.

Does anyone have a quick code example or link to a good video on this? I tried with pointers but I’m not understanding how it helps, I still see zeros.

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nobbz/32/3096_2.png) [@NobbZ](https://forum.golangbridge.org/u/NobbZ)\
**Post date:** [May 26, 2020, 12:44pm UTC](https://forum.golangbridge.org/t/i-need-my-nulls/19097/2 "2020-05-26T12:44:59Z")

</div>

```go
type foo struct {
  Foo *int `json:"foo"`
}

```

---

<div class="post-metadata">

**Author:** ![nellfs](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nellfs/32/15440_2.png) [@nellfs](https://forum.golangbridge.org/u/nellfs)\
**Post date:** [April 25, 2025, 2:22am UTC](https://forum.golangbridge.org/t/i-need-my-nulls/19097/4 "2025-04-25T02:22:30Z")

</div>

Hi Sivan,

Just like NobbZ said, you can use a pointer that indicates that the data might not be present

```auto
type Foo struct {
  Bar *string `json:"bar"`
}

```

Just remember that if you are using a value and it might be null you need to make a null check before using the value as a existent value, otherwise you’ll get a **panic: runtime error: invalid memory address or nil pointer dereference** , you can use something like that:

```auto
var foo Foo

//...unmarshall to Foo

if foo.Bar != nil {
// using len() here is safe
fmt.Println(len(*foo.Bar))
}

```

You might also want to check `omitempty` and `omitzero` JSON tags, for cleaning up your JSON outputs, so search a bit about them (`omitzero` was introduced in go 1.24 so it’s pretty new, you might not find a lot of resources about it)

---

<div class="post-metadata">

**Author:** ![falco467](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/falco467/32/13340_2.png) [@falco467](https://forum.golangbridge.org/u/falco467)\
**Post date:** [April 28, 2025, 7:42am UTC](https://forum.golangbridge.org/t/i-need-my-nulls/19097/5 "2025-04-28T07:42:45Z")

</div>

One nice thing about go is `len()` being a static function. You don’t need a null check on slices before reading length or even before looping over it. A `nil`-slice will behave like a slice of length 0 for most purposes.

But you are of course right for all other primitives, structs and maps.
