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 Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

1 Response

  1. May 29, 2022

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

Leave a Reply

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

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading