Ad space (banner)
๐ŸนGo Lessons
Lesson 43 / 68

Reversing a String and Checking for Palindromes

In this lesson you will learn how to reverse a string in Go and check whether it is a palindrome, covering string handling with the rune type. This is for anyone searching for "Go reverse string palindrome check".

To handle characters correctly, convert the string to []rune before reversing. A string that reads the same forwards and backwards is called a palindrome.

The sample code converts the string to a slice of runes with []rune(s) and reverses it by swapping values from both ends towards the middle, then compares with the original. "racecar" comes out the same either way, so it is reported as a palindrome.

A common stumbling block for beginners is that multi-byte characters break when handled as byte-oriented []byte. Being conscious of the difference between bytes, runes, and characters is the first step to handling Go strings correctly.

You will rarely reverse a string in production, but as practice at working through text one character at a time it is a classic learning exercise.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Go directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)