Skip to content

Instantly share code, notes, and snippets.

@kjk
Last active November 10, 2019 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjk/01707360f4f09f363b7c55f37af0fd8c to your computer and use it in GitHub Desktop.
Save kjk/01707360f4f09f363b7c55f37af0fd8c to your computer and use it in GitHub Desktop.
trimEmptyLinesFromEnd (made with https://codeeval.dev)
package main
import (
"fmt"
"strings"
)
func trimEmptyLinesFromEnd(a []string) []string {
for len(a) > 0 {
lastIdx := len(a) - 1
s := strings.TrimSpace(a[lastIdx])
if len(s) > 0 {
return a
}
a = a[:lastIdx]
}
return a
}
func main() {
a := []string{"a", "b", "", ""}
fmt.Printf("before: %#v\n", a)
a = trimEmptyLinesFromEnd(a)
fmt.Printf("after: %#v\n", a)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment