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...

How to remove the first character of a string in Golang

How to remove the first character of a string in Golang

Learn how to remove first the character of a string in GoLang

Go Strings Cheat Sheet Part 1

Go Strings Cheat Sheet Part 1

Learn the Basic of Working with GoLang Strings.

How to Convert GoLang Integer to String Conversion

How to Convert GoLang Integer to String Conversion

Learn how to convert integers to strings and how to convert strings to integers in GoLang