GoLang 101 — (4) Understanding The Go String Data Types

Handhika Yanuar Pratama
4 min readApr 1, 2024

--

Photo by Chinmay B on Unsplash

Yeah, Day 5

You may wonder why I should divide the data types into several parts. This is because it has different use cases. If I force all of them to become one, the articles will become too long and not interesting to read.

Strings

If you think I am already using this in (2), you are right; we are already writing “Hello World”. Strings are sequences of characters enclosed within double quotes (“”).

In Go, strings are immutable, meaning their values cannot be changed once created. Go provides extensive support for string manipulation and formatting.

Code Program

Here, I write a string data types

After I am running it, it will be running like this

It is as simple as that, but we will not stop here; strings have usable functions; let me tell you more about them.

Some Strings Functions

As immutable sequences of bytes, and the Go standard library provides a rich set of functions for working with strings. Here are some commonly used functions for string manipulation in Go:

(a) Len()

Returns the length of a string in bytes.

package main

import "fmt"

func main(){
fmt.Println("Length: ", len("Handhika"))
fmt.Println("Length: ", len("Handhika Yanuar"))
fmt.Println("Length: ", len("Handhika Yanuar Pratama"))
}

The result will appear as follows:

(b) Strings.HasPrefix

Checks whether a string starts with a specified prefix.

package main

import "fmt"
"strings"

func main(){
fmt.Println("Prefix: ", strings.HasPrefix("Handhika Yanuar Pratama", "Handhika"))
fmt.Println("Prefix: ", strings.HasPrefix("Handhika Yanuar Pratama", "Pratama"))
}

The result will appear as follows:

(c) Strings.HasSuffix

Checks whether a string ends with a specified suffix.

package main
import "fmt"
"strings"
func main(){
fmt.Println("Suffix: ", strings.HasSuffix("Handhika Yanuar Pratama", "Handhika"))
fmt.Println("Suffix: ", strings.HasSuffix("Handhika Yanuar Pratama", "Pratama"))
}

The result will appear as follows:

(d) Strings.Contains

Checks whether a string contains a specified substring.

package main
import "fmt"
"strings"
func main(){
fmt.Println("Contains: ", strings.Contains("Handhika Yanuar Pratama", "Yanuar"))
}

The result will appear as follows:

(e) Strings.Index

Returns the index of the first occurrence of a substring within a string, or -1 if the substring is not found.

package main
import "fmt"
"strings"
func main(){
// indexing
fmt.Print("Index number of Pratama: ", strings.Index("Handhika Yanuar Pratama", "Pratama"))
// just space
fmt.Println("")
fmt.Print("Index number of Pratama: ", strings.Index("Handhika Yanuar Pratama", "Flower"))
}

The result will appear as follows:

(f) Strings.ToLower

Convert a string to lowercase

package main
import "fmt"
"strings"
func main(){
fmt.Println("Result: ", strings.ToLower("hanDHIKA yANUAR Pratama"))
}

The result will appear as follows:

(g) Strings.ToUpper

Convert a string to uppercase

package main
import "fmt"
"strings"
func main(){
fmt.Println("Result: ", strings.ToUpper("handhika yanuar pratama"))
}

The result will appear as follows:

(e) “strings”[number]

Gather characters from specified positions.

package main
import "fmt"

func main(){
fmt.Println("The index of [0]: ", "Handhika Yanuar Pratama"[0])
}

The result will appear as follows:

It may be confusing why the index 0 is 72, which corresponds to ‘H’, because it represents the byte value of ‘H’. The question then arises: how does one convert this byte value back into a string?

Well, let’s learn more about it next time.

Recap

These are just a few examples of the functions available in the strings package. The Go standard library offers many more functions for manipulating and working with strings, covering a wide range of tasks such as splitting, joining, replacing, trimming, and more. Thanks for reading.

--

--

Handhika Yanuar Pratama
Handhika Yanuar Pratama

Written by Handhika Yanuar Pratama

Live the Way the Life ask for || A Stoic Engineer || Technical Writer || Runner x Dreamer

No responses yet