Go programming – Strings

In the last post of this series we installed the go language and tried hello world example. In this post we will go in to further concepts. The following are the keywords used in the Go language and they may not be used as a identifier.

break     default       func   interface  select
case      defer         go     map        struct
chan      else          goto   package    switch
const     fallthrough   if     range      type
continue  for           import return     var

Ok now lets start with strings as a start and see how to Go handles strings.

package main
import "fmt"
func main() {
VariableStr:="This is a Test Variable"
fmt.Println(VariableStr)
}

VariableStr:=”This is a Test Variable”

The variables in Go language are assigned using := combination.  This program will output This is a Test Variable.


package main
import "fmt"
func main() {
VariableStr:="This is a Test Variable"
VariableStr1:="This is a Test Variable"
VariableStr2:=VariableStr+"  "+VariableStr1
fmt.Println(VariableStr2)
}
Output:This is a Test Variable This is a Test Variable

You can concatenate the string using + operator.

package main
import "fmt"
func main(){
VariableStr:="This is a Test Variable"
VariableSubstr:=VariableStr[2:5]

fmt.Println(VariableSubstr)
fmt.Println(VariableStr[:4])
fmt.Println(VariableStr[8:])
}

Output:

is
This
a Test Variable

In Go there is no substring function but we can slice the string. The slices can be sliced using first index and last index.

fmt.Println(VariableStr[:4])

If you dont give the first index then it starts with 0  , similarly

fmt.Println(VariableStr[8:]) if you dont provide the end index then its length of the string.

package main
import "fmt"
func main(){

VariableStr:="This is a Test Variable"
VariableSubstr:=VariableStr[2:5]

fmt.Println(VariableSubstr)

fmt.Println(VariableStr[:4])

fmt.Println(VariableStr[8:30])

}

Here if you provide a index which is out of range then the go generates a run time error.

is
This
panic: runtime error: slice bounds out of range

goroutine 16 [running]:
runtime.panic(0x4b6300, 0x540ccf)
 /usr/lib/go/src/pkg/runtime/panic.c:279 +0xf5
main.main()
 /home/code/Go/GoLangSeries/Strings/fourth.go:12 +0x24a
goroutine 19 [finalizer wait]:
runtime.park(0x412eb0, 0x544450, 0x542f69)
 /usr/lib/go/src/pkg/runtime/proc.c:1369 +0x89
runtime.parkunlock(0x544450, 0x542f69)
 /usr/lib/go/src/pkg/runtime/proc.c:1385 +0x3b
runfinq()
 /usr/lib/go/src/pkg/runtime/mgc0.c:2644 +0xcf
runtime.goexit()
 /usr/lib/go/src/pkg/runtime/proc.c:1445
exit status 2

So far we used the standard feature of the Golang, now lets try to use the string package which comes part of the Go and explore more string based operation.

package main
import "fmt"
import "strings"

func main(){
VariableStr:="This is a Test Variable"
fmt.Println(strings.Count(VariableStr,"i"))
fmt.Println(strings.ContainsAny(VariableStr,"z"))
fmt.Println(strings.HasSuffix(VariableStr,"ble"))
fmt.Println(strings.Repeat(VariableStr,2))
fmt.Println(strings.ToLower(VariableStr))
}

 

Output:

3
false
true
This is a Test VariableThis is a Test Variable
this is a test variable

Explanation:

strings.Count(VariableStr,”i”) will return the count of occurance of the second parameter.

strings.Repeat(VariableStr,2) will repeat the string as many times it you specify.

For more information about the string package refer to https://golang.org/pkg/strings/ .

You can download the source code from https://github.com/codelectron/GoLangSeries .

2 thoughts on “Go programming – Strings

Leave a Reply

Your email address will not be published. Required fields are marked *