Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.74 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "time"
  5.  
  6.     "log"
  7.  
  8.     "fmt"
  9.  
  10.     "github.com/jinzhu/gorm"
  11.     _ "github.com/jinzhu/gorm/dialects/postgres"
  12. )
  13.  
  14. type Product struct {
  15.     gorm.Model
  16.     Code     string
  17.     Birthday time.Time
  18. }
  19.  
  20. func main() {
  21.     db, err := gorm.Open("postgres", "host=localhost user=sdp dbname=sdp sslmode=disable password=sdp")
  22.     if err != nil {
  23.         log.Print(err)
  24.         panic("failed to connect database")
  25.     }
  26.     defer db.Close()
  27.  
  28.     //db.Set("timezone", "Asia/Tehran")
  29.     db.LogMode(true)
  30.  
  31.     // Read
  32.     var product Product
  33.     db.First(&product, 1) // find product with id 1
  34.  
  35.     // Update - update product's price to 2000
  36.     fmt.Println(product.Birthday)
  37.     db.Model(&product).Update("Birthday", time.Now().Add(time.Hour))
  38.     fmt.Println(product.Birthday)
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement