Web Development

The Go Language String Package contains string-related functionalities. Here are samples of what Go Lang’s String Package can do:

Table of Contents

Contains

Checks if the second argument is found within the first argument

package main

import (
  "fmt"
  "strings"
)

func main() {
   fmt.Println("Contains", strings.Contains("great", "eat"))
}
Output
Contains true

Count

Counts the number of instances of a the second argument in the first argument. Take note that this is case sensitive.

package main

import (
  "fmt"
  "strings"
)

func main() {
   fmt.Println("Count", strings.Count("Philippines", "p"))
}
Output
Count 2

HasPrefix

Checks if the first argument begins with the second argument string. Take note that this is case sensitive.

package main

import (
  "fmt"
  "strings"
)

func main() {
   fmt.Println("Starts with?", strings.HasPrefix("pineapple", "pine"))
   fmt.Println("Starts with?", strings.HasPrefix("pineapple", "milk"))
}
Output
$ go run has_prefix.go 
Starts with? true
Starts with? false

Repeat

Duplicate the first argument by the second argument

package main

import (
  "fmt"
  "strings"
)

func main() {
   fmt.Println("Repeat", strings.HasPrefix("go ", 20))
}
Output
$ go run repeat.go 
Repeat go go go go go go go go go go go go go go go go go go go go