Sitemap
Rahul Sid Patil

Rahul Sid Patil

Author of 'Let Us Go' • AI-Native Engineering Researcher • Technologist • Entrepreneur

Golang Quick Reference: Strings

2 min readNov 8, 2023

--

Introduction

Strings are essential data types in any programming language, and Go provides robust support for string manipulation.

String Literals and Raw String Literals

In Go, strings are a sequence of bytes representing characters. Let’s start by understanding the different ways to declare and initialize strings using string literals and raw string literals.

String Literals

String literals are enclosed in double quotes, and they allow you to use escape sequences like \n for newline or \t for tab.

package main

import "fmt"

func main() {
message := "Hello, Go!"
fmt.Println(message)
}

In the code above, message is a string literal.

Raw String Literals

Raw string literals are enclosed in backticks and are useful when you want to include special characters or multiline strings without worrying about escape sequences.

package main

import "fmt"

func main() {
message := `This is a raw string literal.
It can span multiple lines
without escape sequences like \n.`
fmt.Println(message)
}

String Concatenations and Conversions

Now let’s explore how to concatenate strings and perform conversions between strings and other data types.

String Concatenation

String concatenation in Go is simple. You can use the + operator to combine strings.

package main

import "fmt"

func main() {
firstName := "John"
lastName := "Doe"
fullName := firstName + " " + lastName
fmt.Println(fullName)
}

String Conversions

To convert other data types to strings, you can use the fmt.Sprintf function or type conversion.

package main

import (
"fmt"
"strconv"
)

func main() {
age := 30
ageStr := fmt.Sprintf("Age: %d", age)
fmt.Println(ageStr)

score := 95.5
scoreStr := strconv.FormatFloat(score, 'f', 2, 64)
fmt.Println("Score: " + scoreStr)
}

Calculating String Length

In Go, the built-in len function is used to calculate the length of a string, but it doesn't work as expected when dealing with multi-byte characters.

// str-len.go

package main

import (
"fmt"
"unicode/utf8"
)

func main() {
text := "Hello, 你好"
length := len(text) // Incorrect length
runeLength := utf8.RuneCountInString(text) // Correct length
fmt.Printf("Incorrect Length: %d\n", length)
fmt.Printf("Correct Length: %d\n", runeLength)
}
golangda@LAPTOP-60EDIUVV:str-len$ go run str-len.go 
Incorrect Length: 13
Correct Length: 9

In the code above, len(text) gives the incorrect length because it counts bytes, whereas utf8.RuneCountInString(text) correctly counts the number of runes (characters) in the string. We will discuss runes in the next article.

Get Rahul Sid Patil’s stories in your inbox

Join Medium for free to get updates from this writer.

That’s it for our quick reference on working with strings in Go. Strings are fundamental in programming, and understanding their nuances will help you write more efficient and error-free code.

Happy coding!

Note: This article is part of my Golang: Quick Reference series for budding gophers.

Connect with the author

--

--

Author of 'Let Us Go' • AI-Native Engineering Researcher • Technologist • Entrepreneur