The Go Language – exploring Strings – Part 4.2

Hi All,
We are continuing Go language series. In following previous articles we explored introduction to Go, wrote our first Go program, went through basic data types in Go and started with exploring strings in Go
- The Go Language – Introduction Part 1
- The Go Language – Writing first program
- The Go Language – basic data types – Part 3
- The Go Language – exploring Strings – Part 4.1
In this article we will discuss more about string in Go. We will explore two packages – strings and strconv
Take away from this article
- We will got to know details regarding Strings types in Go
- strings package
- strconv package
Details :
strings package
- Go language have “strings” package which contains functions to work with strings
- Few listings of function from “strings” package are as in below fig

- Complete list of string functions from strings package – strings package – strings – pkg.go.dev
- Lets discuss / demonstrate few functions :
- Compare – compares two strings lexicographically. (Strings can be also compared with comparison operators like == , <, !=). comparison is happening byte by byte
func Compare(string1, string2 string) int

- In visual studio code we could see intellisense for all the respective methods
strconv package
- strconv package contain the method for converting string to the other basic data types as shown in below fig
- Complete list of functions from strconv package – https://pkg.go.dev/strconv

- Lets discuss one method – ParseInt –
func ParseUint(strSringToConver string, base int, bitSize int) (uint64, error)

Following is the simple program to demonstrate ParseInt() from strconv package
//Program to explore string conversion functions - strconv package
//main package
package main
//import require packages
import (
"fmt"
"strconv"
)
//main method
func main() {
//Initialize the string with number
strNumber := "12345"
fmt.Println("Demonstration of string conversion functions")
//convert given string to number
//directly printing the result - it will return in format - (i int, err error)
fmt.Println("Result of - strconv.ParseInt(strNumber, 0, 0)")
fmt.Println(strconv.ParseInt(strNumber, 0, 0))
//we could have result in variables as well as in below snap
number, err := strconv.ParseInt(strNumber, 0, 0)
//check whether string is successfully converted or not
if err == nil {
fmt.Println(number)
} else {
fmt.Println(err)
}
} //main
output of the above program will be
Result of - strconv.ParseInt(strNumber, 0, 0)
12345 <nil>
12345
Few Important Points
- A string is immutable sequence of bytes
- For compilers, structure of string is
type _string struct {
elements *byte
len int
}
- string can be concatenated with + or += operators
- By default strings in Go are UTF-8 encoded
Thanks for reading !!! Feel free to discuss in case any suggestions / thoughts / questions
HAVE A GREAT TIME AHEAD!!!
1 Response
[…] The Go Language – exploring Strings – Part 4.2 […]
You must log in to post a comment.