[SOLVED] Help understanding isset

Hi guys,

I need some help understand isset and technical “data type” for .Site:

The isset docstring says:

Takes either a slice, array or channel and an index or a map and a key as input.

So looks like isset takes 2 arguments:

  1. slice, array or channel-and-index or a map and
  2. a key

{{ if isset .Site.Params "isso" }} works…

So,

  • Probably .Site.Params is one of: slice, array or channel-and-index or map. What is it exactly?
  • “isso” is the key.

But the same way, {{ if isset .Site "DisqusShortname" }} does not work. Why?

  • If .Site is none of: slice, array,channel-and-index, map, then what is it?
  • Isn’t "DisqusShortname" a key in .Site?

On another note, can this:

Takes either a slice, array or channel and an index or a map and a key as input.

be rephrased to something like below for clarity? Or something more correct than below, because the multiple ands and ors look confusing and I can’t tell which phrase is for 1st arg and which for 2nd.

Takes 2 input arguments: (1) either a slice, array or channel-and-index or a map, and (2) a key.


Solution

The problem was 2-fold:

  1. I was using isset for an invalid variable type. Apparently the DisqusShortname datatype cannot be used as the 2nd arg to isset.
  2. As someone not knowing the go-lang, this was not obvious. So hugo should have thrown an error if a user like me made such a mistake. This will be fixed in the 0.20 release, thanks to @moorereason (PR 3093).

After the above PR merge in the master branch, if I do isset .Site "DisqusShortname", I get:

ERROR 2017/03/01 18:19:13 Error while rendering “page posts/installing-go-toolchain.md”: template: /home/kmodi/hugo/kaushalmodi.gitlab.io/themes/refined/layouts/_default/baseof.html:45:10: executing “after_main” at <isset .Site "DisqusS…>: error calling isset: unsupported type “ptr”

So the solution is to use {{ with .Site.DisqusShortname }} instead.

DisqusShortcode is a pre-defined string field on the SiteInfo struct. Therefore, it’s always defined. It’s default value is "".

If it were a pointer to a string (*string), you could use isset as you’re expected (since the default value would be nil), but it’s not a pointer.

I agree that it’s not intuitive, but that’s how it works at the moment.

Thanks for the reply.

DisqusShortcode is a pre-defined string field on the SiteInfo struct

Thanks for that link. Even though I don’t code in Go, your explanation and that linked struct makes sense.

Now, {{ if isset .Site "DisqusShortname" }} always evaluates to false… that if block is never executed.

If it were a pointer to a string (*string), you could use isset as you’re expected (since the default value would be nil), but it’s not a pointer.

Then, shouldn’t that raise an error when running hugo, instead of leading the user to believe that all their template code is fine? How will a user know if that isset is logically false or always false because it got used with a var that it shouldn’t have been used with?

1 Like