Skip to content

Instantly share code, notes, and snippets.

@Membucket
Last active July 23, 2018 19:09
Show Gist options
  • Save Membucket/500ee6bb75847ddce66e45dba7c1eca0 to your computer and use it in GitHub Desktop.
Save Membucket/500ee6bb75847ddce66e45dba7c1eca0 to your computer and use it in GitHub Desktop.
Postgres in Go
package main
import (
"github.com/go-pg/pg"
"github.com/go-pg/pg/orm"
"time"
)
type Record struct {
Id int64
Created time.Time
Updated time.Time
}
type Post struct {
Record // easy code reduction
Title string
Body string
}
func (p Post) String() string {
return fmt.Sprintf("Post<%d %s %s %s %s>", p.Id, p.Created.Format(time.RFC3339), p.Updated.Format(time.RFC3339), p.Title, p.Body)
}
func main() {
// insert post
p := &post{Title: "Foo", Body: "Hello, World"}
if err := db.Insert(p); err != nil {
panic(err)
}
// get one post
p := new(post)
p.Id = 1
if err := db.Select(p); err != nil {
panic(err)
}
// get all posts
var posts []post
if err := db.Model(&posts).Select(); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment