Skip to content

proposal: text/template: add nil literal #57773

@willfaught

Description

@willfaught

It's counterintuitive to initialize variables that will eventually hold a map or slice value with false or an empty string.

For example:

{{ $map := false }} // Looks like an error
{{ if .foo }}
  {{ $map = .foo.configMap }}
  {{ $map.baz }}

More intuitive:

{{ $map := nil }} // Looks correct
{{ if .foo }}
  {{ $map = .foo.configMap }}
  {{ $map.baz }}

A nil literal would also enable testing for zero values exactly instead of falsey values more broadly:

{{ if $foo }} // If any truthy value
{{ if ne $foo nil }} // If not nil

I couldn't find a previous proposal for this, open or closed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions