» Go: Build a REST API with Gin » 2. Development » 2.3 Data Models

Data Models

Data models represent the structure of the data that the API deals with. These models define the shape of the data entities exchanged between the client and the server.

For example, in this books management api project, a Book model can be defined:

Create model/book.go:

package model

import "time"

// Book represents the structure of a book
type Book struct {
	ID          uint      `json:"id"`
	Title       string    `json:"title"`
	Author      string    `json:"author"`
	PublishedAt string    `json:"published_at"`
	Description string    `json:"description"`
	ISBN        string    `json:"isbn"`
	TotalPages  int       `json:"total_pages"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}
PrevNext