How to reverse a string in Go?

前端 未结 28 2020
天涯浪人
天涯浪人 2020-12-04 06:45

How can we reverse a simple string in Go?

28条回答
  •  孤街浪徒
    2020-12-04 07:38

    It's assuredly not the most memory efficient solution, but for a "simple" UTF-8 safe solution the following will get the job done and not break runes.

    It's in my opinion the most readable and understandable on the page.

    func reverseStr(str string) (out string) {
        for _, s := range str {
            out = string(s) + out
        }
    
        return
    }
    

提交回复
热议问题