Reverse Strings in Go

Reverse Strings in Go

Learn How To Reverse Strings in GoLang

In this tutorial, we will learn how to reverse a string in Go.

Using a Rune

In this example, we will using a rune to reverse the string:

func ReverseString(str string) string {
	// Create a rune array from the string
	chars := []rune(str)

	// Reverse the string
	for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 {
		chars[i], chars[j] = chars[j], chars[i]
	}
	// return the reverse string
	return string(chars)
}

Source Code

You can find the source code here: GitHub

Did you like it? Why don't you try also...

Is String a Palindrome?

Is String a Palindrome?

Learn How to Test if a String is a Palindrome in Go.

How to Find the Maximum Integer Value In a GoLang Slice

How to Find the Maximum Integer Value In a GoLang Slice

Learn to Find the Maximum Integer Value In a GoLang Slice

Go Strings Cheat Sheet Part 1

Go Strings Cheat Sheet Part 1

Learn the Basic of Working with GoLang Strings.