Standard Library Utilities
Strings Helpers
The strings package has small helpers for searching, replacing, and changing text.
string helper
A string helper is a library function that performs a common text operation.
Strings Helpers
strings_helpers.go
Replay: real traced execution (multi-file project)
package main
import (
"fmt"
"strings"
)
func main() {
var phrase = "go basics"
hasGo := strings.Contains(phrase, "go")
upper := strings.ToUpper(phrase)
slug := strings.ReplaceAll(phrase, " ", "-")
fmt.Println("phrase=", phrase)
fmt.Println("hasGo=", hasGo)
fmt.Println("upper=", upper)
fmt.Println("slug=", slug)
}
package main
import (
"fmt"
"strings"
)
func main() {
var phrase = "trace replay"
hasGo := strings.Contains(phrase, "go")
upper := strings.ToUpper(phrase)
slug := strings.ReplaceAll(phrase, " ", "-")
fmt.Println("phrase=", phrase)
fmt.Println("hasGo=", hasGo)
fmt.Println("upper=", upper)
fmt.Println("slug=", slug)
}
package main
import (
"fmt"
"strings"
)
func main() {
var phrase = "hello go"
hasGo := strings.Contains(phrase, "go")
upper := strings.ToUpper(phrase)
slug := strings.ReplaceAll(phrase, " ", "-")
fmt.Println("phrase=", phrase)
fmt.Println("hasGo=", hasGo)
fmt.Println("upper=", upper)
fmt.Println("slug=", slug)
}
phrase ← "go basics", hasGo ← true, upper ← "GO BASICS", slug ← "go-basics"
8func main() {9 var phrase→ "go basics" = "go basics" //@phrase="trace replay", "hello go"10 hasGo→ true := strings.Contains(phrase"go basics", "go")11 upper→ "GO BASICS" := strings.ToUpper(phrase"go basics")12 slug→ "go-basics" := strings.ReplaceAll(phrase"go basics", " ", "-")1314 fmt.Println("phrase=", phrase"go basics")15 fmt.Println("hasGo=", hasGotrue)16 fmt.Println("upper=", upper"GO BASICS")17 fmt.Println("slug=", slug"go-basics")18}outputphrase= go basics hasGo= true upper= GO BASICS slug= go-basics
phrase ← "trace replay", hasGo ← false, upper ← "TRACE REPLAY"
8func main() {9 var phrase→ "trace replay" = "trace replay"10 hasGo→ false := strings.Contains(phrase"trace replay", "go")11 upper→ "TRACE REPLAY" := strings.ToUpper(phrase"trace replay")12 slug→ "trace-replay" := strings.ReplaceAll(phrase"trace replay", " ", "-")1314 fmt.Println("phrase=", phrase"trace replay")15 fmt.Println("hasGo=", hasGofalse)16 fmt.Println("upper=", upper"TRACE REPLAY")17 fmt.Println("slug=", slug"trace-replay")18}outputphrase= trace replay hasGo= false upper= TRACE REPLAY slug= trace-replay
phrase ← "hello go", hasGo ← true, upper ← "HELLO GO", slug ← "hello-go"
8func main() {9 var phrase→ "hello go" = "hello go"10 hasGo→ true := strings.Contains(phrase"hello go", "go")11 upper→ "HELLO GO" := strings.ToUpper(phrase"hello go")12 slug→ "hello-go" := strings.ReplaceAll(phrase"hello go", " ", "-")1314 fmt.Println("phrase=", phrase"hello go")15 fmt.Println("hasGo=", hasGotrue)16 fmt.Println("upper=", upper"HELLO GO")17 fmt.Println("slug=", slug"hello-go")18}outputphrase= hello go hasGo= true upper= HELLO GO slug= hello-go