Strings in Go

Strings in Go

In this tutorial, we are going to discuss strings in Go language. Like many other programming languages, the string is also one important kind of type in Go.

Strings in Go

A string is a sequence of characters (letters, numbers, symbols) that can be either a constant or a variable.

Strings deserve a special mention in the Go language as they are different in implementation compared to other languages.

In Go language, strings are different from other languages like Java, C++, Python, etc. it is a sequence of variable-width characters where every character is represented by one or more bytes using UTF-8 Encoding.

Due to UTF-8 encoding, the Go language string can contain a text which is the mixture of any language present in the world, without any confusion and limitation of the page. Generally, strings are enclosed in double-quotes ” “, as shown in the below example:

package main

import "fmt"

func main() {
	message := "Welcome to Waytoeasylearn Go lang tutorials"
	fmt.Println(message)
	author := "Ashok Kumar"
	fmt.Println(author)
}

Output

Welcome to Waytoeasylearn Go lang tutorials
Ashok Kumar

Run in playground

Accessing individual bytes of a string

Since a string is a slice of bytes, it’s possible to access each byte of a string.

package main

import "fmt"

func main() {
	message := "Waytoeasylearn"
	fmt.Println("String: ", message)

	fmt.Printf("Bytes: ")

	for i := 0; i < len(message); i++ {

		fmt.Printf("%x ", message[i])
	}
}

Output

String:  Waytoeasylearn 
Bytes: 57 61 79 74 6f 65 61 73 79 6c 65 61 72 6e 

Run in playground

Accessing individual characters of a string

Let’s modify the above program a little bit to print the characters of the string.

package main

import "fmt"

func main() {
	message := "Waytoeasylearn"
	fmt.Println("String: ", message)

	fmt.Printf("Characters: ")

	for i := 0; i < len(message); i++ {
		fmt.Printf("%c ", message[i])
	}

}

Output

String:  Waytoeasylearn 
Characters: W a y t o e a s y l e a r n 

Run in playground

String comparison

The == operator is used to compare two strings for equality. If both the strings are equal, then the result is true; else, it’s false.

package main

import "fmt"

func main() {
	str1 := "Go"
	str2 := "Go"
	if str1 == str2 {
		fmt.Printf("%s and %s are equal\n", str1, str2)
	} else {
		fmt.Printf("%s and %s are not equal\n", str1, str2)
	}

	str3 := "hello"
	str4 := "world"
	if str3 == str4 {
		fmt.Printf("%s and %s are equal\n", str3, str4)
	} else {
		fmt.Printf("%s and %s are not equal\n", str3, str4)
	}

}

Output

Go and Go are equal 
hello and world are not equal

Run in playground

String concatenation

There are multiple ways to perform string concatenation in the Go language. Let’s look at a couple of them.

The most straightforward way to perform string concatenation is using the + operator.

package main

import "fmt"

func main() {
    string1 := "Waytoeasylearn"
    string2 := "is awesome"
    result := string1 + " " + string2
    fmt.Println(result)
}

Output

Waytoeasylearn is awesome

Run in playground

The second way to concatenate strings is using the Sprintf function of the fmt package.

The Sprintf function formats a string according to the input format specifier and returns the resulting string. Let’s rewrite the above program using the Sprintf function.

package main

import "fmt"

func main() {
    string1 := "Waytoeasylearn"
    string2 := "is awesome"
    result := fmt.Sprintf("%s %s", string1, string2)
    fmt.Println(result)
}

Output

Waytoeasylearn is awesome

Run in playground

immutable

In the Go language, strings are immutable. Or in other words, strings are read-only. Once a string has been created, we cannot change the value of the string. If you try to change, then the compiler will throw an error.

package main

import (
    "fmt"
)

func main() {
    message := "Waytoeasylearn"
    message[0] = 'a'
    fmt.Println(h)
}

Output

./prog.go:9:7: cannot assign to h[0]

Run in playground

Length of the string

In Golang string, you can find the length of the string using two functions one is len() and another one is RuneCountInString(). The RuneCountInString() function is provided by UTF-8 package, this function returns the total number of rune presents in the string. And the len() function returns the number of bytes of the string.

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	mystr := "Welcome to Waytoeasylearn"

	length1 := len(mystr)
	length2 := utf8.RuneCountInString(mystr)

	fmt.Println("String is :", mystr)
	fmt.Println("Length 1:", length1)
	fmt.Println("Length 2:", length2)
}

Output

String is : Welcome to Waytoeasylearn
Length 1: 25
Length 2: 25

Run in playground

That’s all about the Strings in Go language. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Go language.!!

Strings in Go
Scroll to top