Strings in Go

Strings in Go

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

Strings in Go

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

Strings deserve a special mention in Go language as they are different in implementation when 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 each and every character is represented by one or more bytes using UTF-8 Encoding.

Let’s look at a simple example that creates a string and prints it.

package main

import "fmt"

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

Output

Welcome to Waytoeasylearn Go lang tutorials

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 Go language. Let’s look at a couple of them.

The most simple 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 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

Strings are immutable in Go. Once a string is created it’s not possible to change it.

package main

import (
    "fmt"
)

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

Output

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


Scroll to top