Jay Taylor's notes

back to listing index

Golang template : Use pipe to uppercase string

[web search]
Original source (stackoverflow.com)
Tags: golang go templates stackoverflow.com
Clipped on: 2016-10-28

I want to upper case a string in a golang template using string.ToUpper like :

{{ .Name | strings.ToUpper  }}

But this doesn't works because strings is not a property of my data.

I can't import strings package because the warns me that it's not used.

Here the script : http://play.golang.org/p/7D69Q57WcN

asked Jan 9 '14 at 21:05
Image (Asset 4/5) alt=
manuquentin
154113
   upvote
  flag
This doesn't answer the question, but: if you're rendering HTML with this template, have a think about whether the uppercasing is just presentational. If it is, use CSS's text-transform: capitalize instead – this is even language aware. – djd Jan 10 '14 at 2:49
up vote 18 down vote accepted

Just use a FuncMap like this (playground) to inject the ToUpper function into your template.

import (
    "bytes"
    "fmt"
    "strings"
    "text/template"
)

type TemplateData struct {
    Name string
}

func main() {
    funcMap := template.FuncMap{
        "ToUpper": strings.ToUpper,
    }

    tmpl, _ := template.New("myTemplate").Funcs(funcMap).Parse(string("{{ .Name | ToUpper  }}"))

    templateDate := TemplateData{"Hello"}
    var result bytes.Buffer

    tmpl.Execute(&result, templateDate)
    fmt.Println(result.String())
}
answered Jan 9 '14 at 21:18
Image (Asset 5/5) alt=
Nick Craig-Wood
29.6k56881

Your Answer

asked

2 years ago

viewed

2572 times

active

10 months ago

Blog

Featured on Meta

Technology Life / Arts Culture / Recreation Science Other
  1. Stack Overflow
  2. Server Fault
  3. Super User
  4. Web Applications
  5. Ask Ubuntu
  6. Webmasters
  7. Game Development
  8. TeX - LaTeX
  1. Software Engineering
  2. Unix & Linux
  3. Ask Different (Apple)
  4. WordPress Development
  5. Geographic Information Systems
  6. Electrical Engineering
  7. Android Enthusiasts
  8. Information Security
  1. Database Administrators
  2. Drupal Answers
  3. SharePoint
  4. User Experience
  5. Mathematica
  6. Salesforce
  7. ExpressionEngine® Answers
  8. Cryptography
  1. Code Review
  2. Magento
  3. Signal Processing
  4. Raspberry Pi
  5. Programming Puzzles & Code Golf
  6. more (7)
  1. Photography
  2. Science Fiction & Fantasy
  3. Graphic Design
  4. Movies & TV
  5. Music: Practice & Theory
  6. Seasoned Advice (cooking)
  7. Home Improvement
  8. Personal Finance & Money
  1. Academia
  2. more (8)
  1. English Language & Usage
  2. Skeptics
  3. Mi Yodeya (Judaism)
  4. Travel
  5. Christianity
  6. English Language Learners
  7. Japanese Language
  8. Arqade (gaming)
  1. Bicycles
  2. Role-playing Games
  3. Anime & Manga
  4. Motor Vehicle Maintenance & Repair
  5. more (17)
  1. MathOverflow
  2. Mathematics
  3. Cross Validated (stats)
  4. Theoretical Computer Science
  5. Physics
  6. Chemistry
  7. Biology
  8. Computer Science
  1. Philosophy
  2. more (3)
  1. Meta Stack Exchange
  2. Stack Apps
  3. Area 51
  4. Stack Overflow Talent
site design / logo © 2016 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2016.10.26.4146