Skip to content

Reverse Function for String in Golang?

Yes, a string is a slice then you can iterate over it easily. Take a look at this snippet which helps you to reverse a string input just in four lines of codes:

package main

import "fmt"

func main() {
	stringVariable := "Morteza!"

	fmt.Println(reverse(stringVariable))
}

func reverse(input string) (output string) {
	for _, charachter := range input {
		output = string(charachter) + output
	}

	return
}
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *