Skip to content

Instantly share code, notes, and snippets.

@srfrog
Created August 1, 2014 22:49
Show Gist options
  • Save srfrog/e3ecf0fe10d5a7a11997 to your computer and use it in GitHub Desktop.
Save srfrog/e3ecf0fe10d5a7a11997 to your computer and use it in GitHub Desktop.
StringSlice is used to scan Postgresql's arrays into Go's string arrays
// StringSlice is used to scan Postgresql's arrays into Go's string arrays
type StringSlice []string
func (s *StringSlice) Scan(src interface{}) error {
b, ok := src.([]byte)
if !ok {
return error(errors.New("Scan src was not []bytes"))
}
// removes all quotes and nesting
// XXX: this assumes "{} are not part of the values
cleanUp := func(r rune) rune {
if r == '"' || r == '{' || r == '}' {
return -1
}
return r
}
str := strings.Map(cleanUp, string(b))
strArr := strings.Split(str, ",")
(*s) = StringSlice(strArr)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment