gogo-gormgo-gin

Wrong return value in golang restapi


i am new in GOLANG ... and now I trying create rest api with gin and retrieve data from mysql using GORM...

table structure.

type Movie struct {
    gorm.Model
    Title       string
    Description string
    Duration    time.Duration
    Showtimes   []Showtime
}

this is my handlers...

func ListMovies(c *gin.Context) {
    var movie []model.Movie

    DB.Find(&movie)

    c.Header("Content-Type", "application/json")
    c.JSON(http.StatusOK, movie)
}

and this is my main.go

package main

import (
    "fmt"

    "github.com/gin-gonic/gin"
)

func main() {
    ConnectDatabase()

    router := gin.Default()

    fmt.Println("Server is running...")
    router.GET("/movies", ListMovies)

    // Start server
    router.Run(":8080")
}

and when I run this api then i see this..

[{"ID":2,"CreatedAt":"0001-01-01T00:00:00Z","UpdatedAt":"0001-01-01T00:00:00Z","DeletedAt":null,"Title":"","Description":"","Duration":0,"Showtimes":null},{"ID":5,"CreatedAt":"0001-01-01T00:00:00Z","UpdatedAt":"0001-01-01T00:00:00Z","DeletedAt":null,"Title":"","Description":"","Duration":0,"Showtimes":null},{"ID":8,"CreatedAt":"0001-01-01T00:00:00Z","UpdatedAt":"0001-01-01T00:00:00Z","DeletedAt":null,"Title":"","Description":"","Duration":0,"Showtimes":null}]

but in database

table_data

so my question is why I see this? Do I need some serializer as like in python? When I saw some samples, there is the same what i have now... but it's not working.

many thanks.

In databse I have datatype for column title.. longtext (when I change to varchar, then data I see) for createdat there is datatype datetime(3) and this show these value 0001-01-01T00:00:00Z ..


Solution

  • solved! add parametr ?parseTime=true to db connection.