Skip to content

Instantly share code, notes, and snippets.

@bmatsuo
Last active March 15, 2016 15:33
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 bmatsuo/a2b4c4aa2a110eb0eaf5 to your computer and use it in GitHub Desktop.
Save bmatsuo/a2b4c4aa2a110eb0eaf5 to your computer and use it in GitHub Desktop.
My use case for Go experssion text-objects in vim. This is a fairly dumb example. But the pattern of iteration between different versions of a function (Version1, Version2, and Version3) in not completely uncommon for me. I find myself in situations function argument expressions must be extracted to produce a more complete or general result.
package main
import (
"io"
"os"
"strings"
)
func main() {
Version1()
Version2()
Version3(os.Stdout)
}
// Version1 is my crude first iteration on doing something.
// I use hardcoded files and inline expressions in arguments
// (quick and dirty).
func Version1() {
_, err := io.WriteString(os.Stdout, gettext("foo"))
if err != nil {
panic(err)
}
}
// Version2 begins cleaning things up. I need to do more
// processing on the text (second argument to io.WriteString),
// so it is replaced with a placeholder value `text` which is
// then defined above as a variable (NOTE: text-objects at play).
func Version2() {
text := gettext("foo")
text = strings.TrimSpace(text)
_, err := io.WriteString(os.Stdout, text)
if err != nil {
panic(err)
}
}
// Version3 is approaching a generic, proper solution where
// os.Stdout, the first argument to io.WriteString, is replaced
// by a generic io.Writer w (NOTE: ideally more text-objects
// because "os.Stdout" is multiple words in vim -- e.g. d3w).
func Version3(w io.Writer) {
text := gettext("foo")
text = strings.TrimSpace(text)
_, err := io.WriteString(w, text)
if err != nil {
panic(err)
}
}
func gettext(name string) string {
return " a string with excess space\t"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment