TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang Reverse String

Reverse a string by converting the string to a rune slice. Use a for-loop.
Reverse string. Sometimes in programs we want to reverse the characters in a string. This can generate a key from existing data. A string cannot be directly sorted.Strings
Instead, we must act on the string at the level of its runes or bytes. Runes in Go are most similar to characters. We can convert strings to rune slices and then reverse those.
Example func. Let us start. Here we introduce the reverse() method. This method receives a string and returns another string. We begin by converting the string argument to a slice of runes.Func

Tip: Runes can represent more than one byte. Runes support 2-byte characters. Logically a rune is a character.

For: We use a for-loop, with a decrementing iteration, to access our slice of runes. We build up a slice of the runes in reverse order.

For

Return: Our func returns a string based on the reversed contents of the rune slice. We just use runes internally to process the string.

Golang program that reverses a string, uses runes package main import "fmt" func reverse(value string) string { // Convert string to rune slice. // ... This method works on the level of runes, not bytes. data := []rune(value) result := []rune{} // Add runes in reverse order. for i := len(data) - 1; i >= 0; i-- { result = append(result, data[i]) } // Return new string. return string(result) } func main() { // Test our method. value1 := "cat" reversed1 := reverse(value1) fmt.Println(value1) fmt.Println(reversed1) value2 := "abcde" reversed2 := reverse(value2) fmt.Println(value2) fmt.Println(reversed2) } Output cat tac abcde edcba
Some notes. In Go, we can access the bytes of a string instead of converting the string to a rune slice. And we can reverse these bytes.

But: For a string with 2-byte chars, runes will preserve the data. Accessing bytes will meanwhile cause some corruption.

Opinion: For a simple method like string reversal, keeping the data correct is probably more important than max performance.

A review. Strings can be manipulated in many ways by first converting them to rune slices. We can reorder or modify runes. This is powerful, versatile, and can be used to reverse strings.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf