Quantcast
Channel: User Sergey Ponomarev - Stack Overflow
Viewing all articles
Browse latest Browse all 70

Answer by Sergey Ponomarev for How to reverse a string in Go?

$
0
0

Golang has the slices.Reverse() for byte slices so you have a simple ASCII string then you can convert the string to byte slice, reverse and convert back to string:

str := "abc"strBytes := []byte(str)slices.Reverse(strBytes)reversed := string(strBytes)println(reversed) // prints cba

But please note that you'll have two memory allocations: str->bytes and bytes->str so this shouldn't be used in a performance critical code.But sometimes you may use the byte slice directly so the second allocation won't happen.


Viewing all articles
Browse latest Browse all 70

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>