Hi
I think we can create a package to reverse a string and can use it whenever
needed.
reverse.go
package strutil
func Reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)}
come to your original .go programm and import that above reverse.go util package
package main
import(
"fmt"
"github.com/himanshu/go_code/strutil" . // Path of reverse.go file
)
func main() {
fmt.println(strutil.Reverse("Hello World"))
}
It dosent take too much memory as well
On Saturday, February 15, 2020 at 10:07:15 PM UTC+5:30, Amarjeet Anand
wrote:
>
> Hi
>
> I was wondering why isn't there built-in string reverse function. Is it
> left intentionally because of some reason?
>
> Although strings are immutable in go, there are multiple ways to achieve
> this pretty easily. But having this function inbuilt will save our time
> because we need it quite often.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/7930beeb-885e-4c41-84bf-124f86d062c7%40googlegroups.com.