Go: check if a number is palindrome

Go: check if a number is palindrome

If you are a Go (or Golang) programmer, you can write a simple program to check whether a number is a palindrome. In this article, we will explore how to do it step by step.

A palindromic number is a number that remains the same when its digits are reversed. For example, 121, 1331, and 12321 are all palindromic numbers. If you are a Go (or Golang) programmer, you can write a simple program to check whether a number is a palindrome. In this article, we will explore how to do it step by step.

Before you start writing code in Go, it's important to understand the basic concept behind a palindrome number. The key is understanding how to get the digits of a number and compare them appropriately.

  1. Getting the digits: Given an integer, you need to find a way to get its digits. You can do this by converting the number to a string and then examining the characters in the string one at a time.

  2. Comparing digits: Once you have the digits, you need to compare them to determine whether the number is a palindrome or not. To do this, you can compare the first digit with the last, the second with the second to last and so on up to half the number.

Now that we understand the basic concept, we can proceed with the implementation in Go. Here is an example of Go code to check if a number is a palindrome:


package main

import (
     "fmt"
     "strconv"
)

func isPalindrome(int num) bool {
     // Convert the number to a string
     numStr := strconv.Itoa(num)
    
     // Initialize indices to compare digits
     left, right := 0, len(numStr)-1
    
     for left < right {
         // Compare the matching digits
         if numStr[left] != numStr[right] {
             return false
         }
         // Move to the next pair of digits
         left++
         right--
     }
    
     return true
}

func main() {
     // Let's test the function with some examples
     fmt.Println(isPalindrome(121)) // True
     fmt.Println(isPalindrome(12321)) // True
     fmt.Println(isPalindrome(12345)) // False
}

In this code, we have defined a function isPalindrome that takes an integer as an argument and returns true if the number is palindrome and false otherwise.

The function first converts the number to a string using strconv.Itoa. Then, using two indices (left and right), compare the corresponding digits starting from the outer edges towards the center. If at any point the digits do not match, the function returns false. If the loop goes through all the digits without finding a discrepancy, the number is considered a palindrome and the function will return true.

In main(), we tested the function with some examples to see if it works correctly.

Conclusion

You now have working code for checking whether a number is a palindrome in Go. This is an example of how you can use the Go language to solve a relatively simple mathematical problem. You can further expand this idea and use the same logic to check whether a word is a palindrome or explore other variations of this interesting problem.