Skip to content

Instantly share code, notes, and snippets.

@morrxy
Created August 16, 2017 11:35
Show Gist options
  • Save morrxy/0a99247325dbc477e95e2800f0a02311 to your computer and use it in GitHub Desktop.
Save morrxy/0a99247325dbc477e95e2800f0a02311 to your computer and use it in GitHub Desktop.
[template check empty value] #golang

https://stackoverflow.com/questions/42434592/how-to-check-empty-value-of-a-string-in-golang-template

{{if .source }}
  <div style="display:none;">
    <input id="username" name="source" value="{{ .source }}"/>
    <input id="username" name="target" value="{{ .target }}"/>
  </div>
{{else}}
  <div>empty</div>
{{end}}

https://stackoverflow.com/questions/32774234/show-default-content-in-a-template-if-an-object-is-nil-otherwise-show-based-on-t

  {{if not .}}
     output when . is nil or otherwise empty including
       false, 0, and any array, slice, map, or string of length zero
  {{else if eq .MetaValue "some-x"}}
         // some-x case
  {{else}} 
         // other case
  {{end}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment