I would to change EntryDate format like yyyy-mm-dd formated on the struct,
type Value struct {
Id uint `json:”id”`
EntryDate time.time `json:”entry_date”`
ProductId int `json:"product_id"`
Value float64 `json:”value”`
}
By default the result like this
{
Id: 11,
EntryDate: "2017-11-23T00:00:00Z",
product_id: 1,
Value: 932.3
},
How to change EntryDate format like "yyyy-mm-dd" on the struct without parsing on the code ?
Change the struct type without parsing at loops ? check this article http://sosedoff.com/2016/07/16/golang-struct-tags.html
If I am understanding you correctly you can do this like this:
package main
import (
"encoding/json"
"fmt"
"time"
)
type Value struct {
Id uint `json:”id”`
EntryDate time.Time `json:”entry_date”`
ProductId int `json:"product_id"`
Value float64 `json:”value”`
}
func main() {
val := Value{}
val.Id = 1
val.EntryDate = time.Now().UTC()
val.ProductId = 2
val.Value = 1.223
t := val.EntryDate.UTC().Format("2006-01-02")
fmt.Println("formated time : ", t)
b, err := json.Marshal(val)
if err != nil {
fmt.Println("failed to marshal object", err)
return
}
fmt.Println("actual object", string(b))
}
Output
formated time : 2009-11-10
actual object {"Id":1,"EntryDate":"2009-11-10T23:00:00Z","product_id":2,"Value":1.223}
Check in Go playground: https://play.golang.org/p/Fc35ealF5BI