Skip to content

Instantly share code, notes, and snippets.

@akhenakh
Last active December 18, 2015 17:19
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 akhenakh/5818109 to your computer and use it in GitHub Desktop.
Save akhenakh/5818109 to your computer and use it in GitHub Desktop.
Learning Golang from a Pythonista if "foo" in alist:
// lookup for the users table create it otherwise
// var tables []string and StringSlice are equivalent
var tables sort.StringSlice
err = r.TableList().Run(session).All(&tables)
if err != nil {
log.Panic(err)
}
// I miss python if "users" in tables
exists := false
for _, element := range tables {
if element == "users" {
exists = true
break
}
}
if !exists {
err = r.TableCreate("users").Run(session).Exec()
if err != nil {
log.Panic(err)
}
}
// but you still can
sort.Sort(tables)
if sort.SearchStrings(tables, "users") != 0 {
err = r.TableCreate("users").Run(session).Exec()
if err != nil {
log.Panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment