Recently, I studied the Golang Template engine and found that it is weak in providing many common functions, such as string substitution, judgment, Base64 encoding and decoding, etc. But Golang Template gives us a custom function, template.FuncMap, that allows us to extend our functionality.

func main() {    // First we create a FuncMap with which to register the function.    funcMap := template.FuncMap{        // The name "title" is what the function will be called in the template text.        "title": strings.Title,    }    // A simple template definition to test our function.    // We print the input text several ways:    // - the original    // - title-cased    // - title-cased and then printed with %q    // - printed with %q and then title-cased.    const templateText = `Input: {{printf "%q" .}}Output 0: {{title .}}Output 1: {{title . | printf "%q"}}Output 2: {{printf "%q" . | title}}`    // Create a template, add the function map, and parse the text.    tmpl, err := template.New("titleTest").Funcs(funcMap).Parse(templateText)if err ! = nil {        log.Fatalf("parsing: %s", err)    }    // Run the template to verify the output.    err = tmpl.Execute(os.Stdout, "the go programming language")if err ! = nil {        log.Fatalf("execution: %s", err)    }}Copy the code

This is an example of a Go Template that extends a title function with strings.Title. We then generate a template.funcmap with the following code:

    funcMap := template.FuncMap{        // The name "title" is what the function will be called in the template text.        "title": strings.Title,    }Copy the code

With funcMap we can register the extended function through the Funcs method of template so that we can use it in the template.

const templateText = `Input: {{printf "%q" .}}Output 0: {{title .}}Output 1: {{title . | printf "%q"}}Output 2: {{printf "%q" . | title}}`Copy the code

This is an example of enhancing the Golang Template with custom functions to extend the capabilities of the Golang Template.

At the beginning, MY idea was to create an open source project through Golang Template, which realized all the commonly used Template functions. However, I was afraid of repeating the wheel, so I went to Github to search whether there was a good library already implemented, but failed to find it (please inform me if you found it).

Before I started writing, I was reminded of HUGO, the open source project that I used to build my own blog. HUGO has built many powerful template functions in order to support blog customization. In this case, we can extract these template functions of HUGO, modify and enhance them, so as to form a tool library for daily use.

And then on a weekend night, in the dead of night, we started working on it, and we ended up pumping out a beta version in about four hours. A few days later, in the dead of night, I improved the relevant code and documentation, and finally opened the source on Github.

Open source libraries address: https://github.com/flysnow-org/soha

Open source libraries address: https://github.com/flysnow-org/soha

This library is very simple to use, first you need to import the library import “github.com/flysnow-org/soha”. Then get a FuncMap using the following function

sohaFuncMap := soha.CreateFuncMap()Copy the code

Having FuncMap is very easy; if you use the built-in template, you just call the Funcs method.

tmpl, err := template.New("titleTest").Funcs(sohaFuncMap).Parse(templateText)Copy the code

If you are using a Golang Web framework such as GIN, just register with the following code:

    router := gin.Default()    sohaFuncMap := soha.CreateFuncMap()    router.SetFuncMap(sohaFuncMap)Copy the code

Here’s a Hello World for your reference.

package mainimport (    "github.com/flysnow-org/soha"    "html/template"    "log"    "os")func main() {    sohaFuncMap := soha.CreateFuncMap()    const templateText = `{{md5 .}}`    tmpl, err := template.New("titleTest").Funcs(sohaFuncMap).Parse(templateText)if err ! = nil {        log.Fatalf("parsing: %s", err)    }    err = tmpl.Execute(os.Stdout, 123456)if err ! = nil {        log.Fatalf("execution: %s", err)    }}Copy the code

SOHA enhances 50 to 60 functions for Golang Template, including strings, urls, conversions, hashes, Base64, Math, collections, and more, to make your Golang Web development more efficient. Please refer to the Github repo library for instructions and examples of each function. There are detailed instructions, both in English and Chinese.

Because SOHA extracted the code from HUGO, SOHA kept HUGO’s Apache Licence. Welcome everyone star, issue, PR and other contributions, use more.

It’s all about not 996 anymore.

Less than 50 lines of code to achieve public two-dimensional code animation
Go is so easy to generate qr codes
Go language (Golang) environment construction details

Sweep yards attention