Strings, Formatting, Text Templates & Regular Expressions in Go 📝

Text and string processing is fundamental to configuration templating, log parsing, code generation, and API responses. Go provides rich text manipulation tools across the strings, fmt, text/template, and regexp packages.


1. The strings Package Functions

The standard library strings package provides high-performance, allocation-conscious string operations:

package main
 
import (
	"fmt"
	"strings"
)
 
func main() {
	s := "cloud-native-kubernetes"
 
	fmt.Println("Contains 'native':", strings.Contains(s, "native")) // true
	fmt.Println("HasPrefix 'cloud':", strings.HasPrefix(s, "cloud")) // true
	fmt.Println("Count of '-':", strings.Count(s, "-"))             // 2
	fmt.Println("ToUpper:", strings.ToUpper(s))
 
	// Splitting and Joining
	parts := strings.Split(s, "-")
	fmt.Printf("Split: %q\n", parts) // ["cloud" "native" "kubernetes"]
	fmt.Println("Joined:", strings.Join(parts, "/")) // cloud/native/kubernetes
 
	// Replacing
	fmt.Println("Replaced:", strings.ReplaceAll(s, "kubernetes", "k8s"))
}

2. String Formatting Verbs (fmt.Printf)

Go’s fmt package uses printf-style verbs. The most important verbs for Go developers are:

VerbMeaningExample Output for User{Name: "Alice", Age: 30}
%vDefault value representation{Alice 30}
%+vStruct field names included{Name:Alice Age:30}
%#vGo syntax representationmain.User{Name:"Alice", Age:30}
%TType of the valuemain.User
%tBoolean (true/false)true
%dBase-10 integer42
%.2fFloating-point with 2 decimals3.14
%qDouble-quoted string with escaping"hello\nworld"
%pPointer address0xc000014070

3. Text & HTML Templates (text/template)

The text/template package parses templates and applies them to data structures (the same templating engine used by Helm charts and Docker CLI formatting):

package main
 
import (
	"os"
	"text/template"
)
 
type Deployment struct {
	Name     string
	Replicas int
	Envs     []string
}
 
func main() {
	tmplText := `
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Name }}
spec:
  replicas: {{ .Replicas }}
  env:
  {{- range .Envs }}
    - name: {{ . }}
  {{- end }}
`
 
	tmpl, err := template.New("k8s").Parse(tmplText)
	if err != nil {
		panic(err)
	}
 
	dep := Deployment{
		Name:     "payment-service",
		Replicas: 3,
		Envs:     []string{"PORT", "DB_URL", "LOG_LEVEL"},
	}
 
	// Render directly to standard output or an HTTP response writer!
	tmpl.Execute(os.Stdout, dep)
}

4. Regular Expressions (regexp)

Go’s regexp package implements the RE2 regular expression engine, which guarantees linear-time execution (preventing catastrophic backtracking ReDoS security vulnerabilities):

package main
 
import (
	"fmt"
	"regexp"
)
 
func main() {
	// MustCompile panics if the regex pattern is invalid (ideal for package-level vars)
	re := regexp.MustCompile(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,}$`)
 
	fmt.Println("Match valid email:", re.MatchString("alex@example.com")) // true
	fmt.Println("Match invalid email:", re.MatchString("invalid-email@"))  // false
 
	// Finding submatches with capture groups
	extractRe := regexp.MustCompile(`([a-zA-Z]+): (\d+)`)
	line := "Order: 1042, User: 998"
 
	matches := extractRe.FindAllStringSubmatch(line, -1)
	for _, m := range matches {
		fmt.Printf("Key: %s, Value: %s\n", m[1], m[2])
	}
}

Across the Wiki