Fitness and everything else

How Determine And Change A Variable's Type In Hugo

Today I had a pretty interesting dilemma. I was working again on my weather station data’s short code for Hugo. I wanted to add the day’s temperature range to the table since today we had a wide range here, and it made me think of it.

It seemed simple enough to just subtract the low from the high. I tried doing just that and kept getting an error. With some debugging, I figured out that although the high and low fields are what look like numbers, they’re cast as strings for some reason.

I added this line to debug it as I built up the variables after doing some googling on how to determine a variable’s type in Hugo. Searching the internet is just so annoying anymore. I specified either golang or gohugo in my searches, and it kept insisting on giving me either java or JavaScript or maybe python.

{{ printf "%T" $VariableName }}

That got me to figure out that I needed to recast those to the floats that I needed, since they did have a decimal in them. That was easy enough using the float function. Once I did that, Hugo would happily do the subtraction that I wanted it to do.

The int function is useful for converting a string to an integer. I came across the need to use it several times

{{ $int := int $VariableName }}

the float function is handy for converting a string variable into a floating point numbers

{{ $float := float $VariableName }}

I have needed to convert something to a string a few times working on my templates. I discovered that I can use the string function for that

{{ $string := string $VariableName }}