If you really just want to reverse rune-by-rune, it's pretty
straightforward:

func Reverse(s string) string {
        r := make([]byte, 0, len(s))
        for len(s) > 0 {
                _, n := utf8.DecodeLastRuneInString(s)
                i := len(s) - n
                r = append(r, s[i:]...)
                s = s[:i]
        }
        return string(r)
}

That will also deal correctly with invalid utf8 encoding - all the bytes of
the original string will be present in the result.


On Wed, 26 Feb 2020 at 14:20, <ffm2...@web.de> wrote:

> Maybe the implementation in Java is something you could steal to save
> time. Have a look into class StringBuilder where there is a reverse()
> method. It does the reversion differently depending on whether dealing with
> UTF16 or not.
>
> Am Samstag, 15. Februar 2020 17:37:15 UTC+1 schrieb Amarjeet Anand:
>>
>> 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 golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/934265f6-666b-446b-aa5e-73f0b2bdcd78%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/934265f6-666b-446b-aa5e-73f0b2bdcd78%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAJhgacjhm5TxaTgN3zwrKuD7YYBoXcpCitCuWeipqV_thhtYFQ%40mail.gmail.com.

Reply via email to