Last active
October 17, 2019 17:59
-
-
Save matteo-grella/d63263157cf28be6fc71f28b00b4796a to your computer and use it in GitHub Desktop.
Reverse string in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func reverse(lst []string) chan string { | |
ret := make(chan string) | |
go func() { | |
for i, _ := range lst { | |
ret <- lst[len(lst)-1-i] | |
} | |
close(ret) | |
}() | |
return ret | |
} | |
func reverse(lst []interface{}, callback func(x interface{})) { | |
for i := len(lst)-1; i >= 0; i-- { | |
callback(lst[i]) | |
} | |
} | |
func reverse(lst []string) { | |
for i := 0; i < len(lst)/2; i++ { | |
j := len(lst) - i - 1 | |
lst[i], lst[j] = lst[j], lst[i] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment