Go – Strings Overview
Estimated reading: 3 minutes 311 views

Go Substring Extraction – Extract Portions of Strings with Slicing (2025 Guide)

Introduction – How to Extract a Substring in Go?

Unlike some other languages, Go does not have a built-in substring() function, but it supports substring extraction using slice syntax. You can use slicing to extract a part of a string by index range, and it’s fully compatible with Go’s UTF-8 string handling via runes.

In this section, you’ll learn:

  • How to extract substrings using slice syntax
  • How to extract based on index, length, or runes
  • How to handle Unicode and multibyte characters
  • Best practices for safe slicing in Go

Basic Substring Syntax Using Slice

s := "Golang"
sub := s[1:4]
fmt.Println(sub)

Output:

ola

s[1:4] extracts characters from index 1 to 3 (4 is excluded).


Extract from Start or to End

s := "Golang"

// From start to index 3
fmt.Println(s[:3]) // Gol

// From index 2 to end
fmt.Println(s[2:]) // lang

Flexible start and end slicing options.


Index Must Be Byte-Aligned

Go strings are UTF-8 encoded. Slicing works on bytes, so slicing through a multibyte (Unicode) character can break the string.

Problem Example:

emoji := "Gopher"
fmt.Println(emoji[0:2]) //  may cause invalid UTF-8 output

Safe Substring with Runes (Unicode-Safe)

Convert to []rune for character-based slicing:

runes := []rune("Gopher")
fmt.Println(string(runes[0:2]))

Output:

G

This safely extracts the first two characters, including multibyte ones.


Extract Last N Characters

s := "GoLang"
last3 := s[len(s)-3:]
fmt.Println(last3) // ang

Use len(s) to get the string length for dynamic slicing.

Use []rune(s) instead of s when handling Unicode characters for such operations.


Example – Dynamic Substring from Input

input := "ExtractThis"
start := 0
end := 7

if end <= len(input) {
    fmt.Println(input[start:end]) // Extract
}

Always check boundaries to avoid slice out-of-range errors.


Summary Table – Substring Extraction Methods

MethodUse CaseUnicode-Safe?
s[start:end]Simple byte slicing No
string([]rune(s)[x:y])Character-based slicing (Unicode-safe) Yes
s[:n] or s[n:]Extract from/to boundary Use with care
len(s)Used to calculate end index Yes

Summary – Recap & Next Steps

Substring extraction in Go is achieved using slice notation, but keep in mind that slicing is byte-based. For safe operations with Unicode or emojis, convert the string into a rune slice.

Key Takeaways:

  • Go uses s[start:end] for substring extraction
  • String slicing is byte-based; may break multibyte characters
  • Use []rune(s) for safe Unicode substring operations
  • Always validate slice bounds to prevent runtime errors

Next: Explore Go String Formatting for clean string output and variable embedding.


FAQs – Go Substring Extraction

Does Go have a substring() method like Java or Python?
No. Use slice syntax: s[start:end] for substring behavior.

How can I safely extract Unicode substrings in Go?
Convert the string to a rune slice: []rune(s)[start:end].

What happens if I slice beyond the string length?
Go will panic with a runtime slice out-of-bounds error.

Can I get the last N characters of a string?
Yes. Use slicing like s[len(s)-n:], and ensure to handle Unicode with runes.

Is string slicing efficient in Go?
Yes. Strings are immutable, but slicing shares the underlying data without copying.


Share Now :
Share

Go Substring Extraction

Or Copy Link

CONTENTS
Scroll to Top