March 2020
Intermediate to advanced
406 pages
8h 39m
English
Being able to manipulate slices of strings is helpful, as we have seen in previous chapters. The Sprig library helps us perform some string slice manipulation actions. In our example, we'll split a string based on the . character.
First, we import the necessary libraries:
package main import ( "fmt" "os" "text/template" "github.com/Masterminds/sprig") func main() {
Next, we split our templated string using the . delimiter:
tpl := `{{$v := "Hands.On.High.Performance.In.Go" | splitn "." 5}}{{$v._3}}` functionMap := sprig.TxtFuncMap() t := template.Must(template.New("String Split").Funcs(functionMap).Parse(tpl)) fmt.Print("String Split into Dict (word 3): ") err := t.Execute(os.Stdout, tpl) if err != nil { fmt.Printf("Couldn't ...Read now
Unlock full access