The models.go file

Starting with the models is always interesting, because it is the best place to understand the business of a project.

At first, we declare the package on which we are working, and then the imports required for the project, as follows:

    package main    import ( 
      "github.com/jmoiron/sqlx" 
      "golang.org/x/crypto/bcrypt" 
    ) 

Then, we make the declaration of our entity, Users. Go does not have classes, but instead uses structs. The final behavior is very similar to the OOP, of course, but there are peculiarities.

  • User is the struct responsible to represent the database entity:
 type User struct { ID int `json:"id" db:"id"` Name string `json:"name" db:"name"` Email string `json:"email" db:"email"` Password string `json:"password" ...

Get Microservice Patterns and Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.