Malformed character constant if using single quote vs double

If I use single quotes this throw error malformed character constant.
{{ hasPrefix '#string' '#' }}

Just wonder if this is standard practice? I wanted to read up about this if it’s in the documentation somewhere?

2 Likes

I believe it is in the go spec: The Go Programming Language Specification - The Go Programming Language

Go uses backticks and double-quotes (snipped excerpt):

There are two forms: raw string literals and interpreted string literals.

Raw string literals are character sequences between back quotes, as in foo . Within the quotes, any character may appear except back quote. The value of a raw string literal is the string composed of the uninterpreted (implicitly UTF-8-encoded) characters between the quotes; in particular, backslashes have no special meaning and the string may contain newlines. Carriage return characters (‘\r’) inside raw string literals are discarded from the raw string value.

Interpreted string literals are character sequences between double quotes, as in "bar" . Within the quotes, any character may appear except newline and unescaped double quote.

I didn’t know this until now, so there may be more to it. I personally always use double quotes by default, so I didn’t run into the same issue. Maybe a go programmer can drop some more knowledge on us. :slight_smile:

2 Likes

I am not a Go developer, but this is just the way how it is :wink: Golang is going a very strict and principled route in everything they do. “Why would we introduce two ways of marking a string, if one is sufficing enough?” is probably within their line of thought.

Some docs everyone should read every now and then to understand more about why things in Hugo templates are the way they are would be the documentation of the fmt-package and the template/html package. It’s hard to understand the first time, but the more often I read through the more it all makes sense. Most of the unexplained details of Hugo hide in there (template logic, comment formats, etc…)

PS: also, in most cases, the answer to “why is this done this way in Golang” is “for security” :slight_smile:

1 Like

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