Skip to content

Improve the return keyword in templates #15212

Closed

Description

@bep

There's a long-living upstream issue about this (golang/go#53015) which I don't think is going to be fixed in Go anytime soon.

We do have some patches to Go's template execution package, so if the fix I have in mind for this isn't too intrusive in that package (as in: it makes it very hard to maintain the fork), I think it's worth to improve this situation.

We have added a return keyword, but it currently have some limiations:

  • It only works in partials.
  • It must be the last command in the template.
  • The current implementation, with its template transformations, has some complexities that would be great to rid of.

I have often wanted to return early, even from regular templates, which often avoids wrapping large block of templates in conditionals.

So, I suggest we have a look if it's feasible to introduce a sentinel error with an optional value (used in partials), so the template func (with the return alias) would be something ala:

func (ns *Namespace) Return(args ...any) ReturnErr {
  var val any = nil
   if len(args) > 0 {
      val = args[0]
   }
   return &ReturnErr(val: val}}
}

And then somehow trapped in the template execution.

Activity

  1. self-assigned this
    on Aug 16, 2026
  2. added this to the v0.166.0 milestone on Aug 16, 2026
  3. pinned this issue on Aug 16, 2026
  4. added 3 commits that reference this issue on Aug 16, 2026
    3b780a5
    5bea058
    8405b80
  5. unpinned this issue on Aug 20, 2026
  6. github-actions commented on Sep 10, 2026

    @github-actions

    This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  7. locked as resolved and limited conversation to collaborators on Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions