The Go Language – exploring Strings – Part 4.2

Exploring Go - strings in Go
Exploring Go - strings in Go

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

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

The Go Language – exploring Strings - importing "strings" package - string methods from "strings" package
fig : The Go Language – exploring Strings – importing “strings” package – string methods from “strings” package
  • 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

The Go Language – exploring Strings - importing "strings" package - string methods from "strings" package - Compare()
fig : The Go Language – exploring Strings – importing “strings” package – string methods from “strings” package – Compare()
  • 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

The Go Language – exploring Strings - importing "stringconv" package - string conversion methods from "stringconv" package
fig : The Go Language – exploring Strings – importing “stringconv” package – string conversion methods from “stringconv” package
  • Lets discuss one method – ParseInt

func ParseUint(strSringToConver string, base int, bitSize int) (uint64, error)

The Go Language – exploring Strings - importing "stringconv" package - string conversion methods from "stringconv" package - exploring ParseInt()
fig : The Go Language – exploring Strings – importing “strconv” package – string conversion methods from “strconv” package – exploring ParseInt()

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!!!

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

1 Response

  1. May 29, 2022

    […] The Go Language – exploring Strings – Part 4.2 […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: