Created
March 5, 2018 13:27
-
-
Save afrikaan-official/2567bc48f81bc429de20c45fa4ffcab3 to your computer and use it in GitHub Desktop.
Pop orm to with custom time type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
"encoding/json" | |
"github.com/gobuffalo/pop" | |
) | |
type MyTime time.Time | |
func (m MyTime) MarshalJSON() ([]byte, error) { | |
//do your serializing here | |
stamp := fmt.Sprintf("\"%s\"", time.Time(m).Format("02.01.2006 15:04:05")) | |
return []byte(stamp), nil | |
} | |
//City struct is | |
type Post struct { | |
ID int `db:"id"` | |
Title string `db:"title"` | |
SendingDate MyTime `json:"send" db:"sending_date"` | |
} | |
//Disrict type as | |
type District struct { | |
ID int `json:"id" db:"id"` | |
Name string `json:"name" db:"district_name"` | |
CityID int `json:"cityId" db:"city_id"` | |
} | |
func main() { | |
var p Post | |
db, err := pop.Connect("test") | |
if err != nil { | |
fmt.Println(err.Error()) | |
} | |
err = db.Find(&p, 10) | |
if err != nil { | |
fmt.Println(err.Error()) | |
} | |
bs, err := json.Marshal(&p) | |
fmt.Println(string(bs)) | |
fmt.Scanln("") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment