# How to check for nil pointer properly?

**URL:** <https://discourse.gohugo.io/t/how-to-check-for-nil-pointer-properly/27061>\
**Category:** support\
**Created:** [July 18, 2020, 3:42pm UTC](https://discourse.gohugo.io/t/how-to-check-for-nil-pointer-properly/27061 "2020-07-18T15:42:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vendelin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vendelin/32/11243_2.png) [@vendelin](https://discourse.gohugo.io/u/vendelin)\
**Post date:** [July 18, 2020, 3:42pm UTC](https://discourse.gohugo.io/t/how-to-check-for-nil-pointer-properly/27061/1 "2020-07-18T15:42:36Z")

</div>

This doesn’t work

```
{{- if eq $m nil -}}
{{ return }}
{{- end -}} 
{{- $cont := chomp $m.Content -}}

```

as `<$m.Content>: nil pointer evaluating resource.Resource.Content` is thrown – exactly what I was trying to avoid.

`{{- if not $m -}}` doesn’t work either.

Apparently I’m missing something that everyone else knows. How to do it properly in a partial?

---

<div class="post-metadata">

**Author:** ![divinerites](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/divinerites/32/12625_2.png) [@divinerites](https://discourse.gohugo.io/u/divinerites)\
**Post date:** [July 18, 2020, 5:30pm UTC](https://discourse.gohugo.io/t/how-to-check-for-nil-pointer-properly/27061/2 "2020-07-18T17:30:00Z")

</div>

1 - May be try `{{ if eq $m "nil" }}`

2 - Not sure if the `{{ return }}` command is legit without return variable. And in any case `{{ return $myvar }}` is evaluate **ONCE** in the partial and is **NOT** affected by the go template logic.

See : [Returning values from partials](https://discourse.gohugo.io/t/returning-values-from-partials/26944/5)

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [July 18, 2020, 6:03pm UTC](https://discourse.gohugo.io/t/how-to-check-for-nil-pointer-properly/27061/3 "2020-07-18T18:03:04Z")

</div>

> [@vendelin](#):
>
> ```auto
> {{- if eq $m nil -}}
> {{ return }}
> {{- end -}} 
> {{- $cont := chomp $m.Content -}}
> 
> ```

Short story: The return need to be the last statement in your template.

So, you need to do something ala:

```auto
{{ $result := "" }}
{{ with $m }}
{{ $result = .Content }}
{{ end }}
{{ return $result }}

```

---

<div class="post-metadata">

**Author:** ![vendelin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vendelin/32/11243_2.png) [@vendelin](https://discourse.gohugo.io/u/vendelin)\
**Post date:** [July 18, 2020, 7:53pm UTC](https://discourse.gohugo.io/t/how-to-check-for-nil-pointer-properly/27061/4 "2020-07-18T19:53:39Z")

</div>

No-no, nil in brackets was my gotcha of yesterday 😆  
Thanks for the link, it was quite an entertaining reading while walking in the local forest. It actually answers my question.

---

<div class="post-metadata">

**Author:** ![vendelin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vendelin/32/11243_2.png) [@vendelin](https://discourse.gohugo.io/u/vendelin)\
**Post date:** [July 18, 2020, 7:56pm UTC](https://discourse.gohugo.io/t/how-to-check-for-nil-pointer-properly/27061/5 "2020-07-18T19:56:00Z")

</div>

Arrogantly have I thought, there is nothing new to learn about return statements. Thanks a bunch, my partial now works they way it is supposed to.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [July 18, 2020, 9:26pm UTC](https://discourse.gohugo.io/t/how-to-check-for-nil-pointer-properly/27061/6 "2020-07-18T21:26:12Z")

</div>

I have seen this “return statement” confusion before – the short story, again, I implemented this crude version of “return” in 2 hours. Doing it the right way would have taken me hundreds, at least.

---

<div class="post-metadata">

**Author:** ![vendelin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/vendelin/32/11243_2.png) [@vendelin](https://discourse.gohugo.io/u/vendelin)\
**Post date:** [July 19, 2020, 10:25am UTC](https://discourse.gohugo.io/t/how-to-check-for-nil-pointer-properly/27061/7 "2020-07-19T10:25:31Z")

</div>

Yes, I’ve read this heated discussion thread, and I don’t want to re-ignite it. You are right: while less than perfect, it is far more useful than nothing. I’m not paying anything for the software, and use it while enjoying very efficient support.

One suggestion, though: since this behavior is rather unusual, the documentation should scream about it, and it won’t take long to accomplish. I might volunteer for the task once I’m done with my theme. Until then – thanks for the feature.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [July 21, 2020, 10:25am UTC](https://discourse.gohugo.io/t/how-to-check-for-nil-pointer-properly/27061/8 "2020-07-21T10:25:36Z")

</div>

Closed on July 21, 2020, 10:25am UTC

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
