Skip to content

Instantly share code, notes, and snippets.

@matteo-grella
Last active October 17, 2019 17:59
Show Gist options
  • Save matteo-grella/d63263157cf28be6fc71f28b00b4796a to your computer and use it in GitHub Desktop.
Save matteo-grella/d63263157cf28be6fc71f28b00b4796a to your computer and use it in GitHub Desktop.
Reverse string in Go
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