Cookies management by TermsFeed Cookie Consent
Russia has invaded Ukraine and already killed tens of thousands of civilians, with many more raped or tortured. It's a genocide. We need your help. Let's fight back against the Russian regime.
Help Ukraine! Fight the Russian regime!

What we are going to build

Please consider supporting us by disabling your ad blocker

5/21

As part of our introduction to PostgreSQL in Go, we will create a mini ranking of websites that we will store in the database. So, Website will be our domain object. In addition to creating and reading objects from the database, we also want to be able to delete and update Website records. Therefore, the application we will make should perform all CRUD (create, read, update, delete) operations.

Database operations will be defined in the Repository interface you have seen before. We will create three concrete implementations of this interface - the first using the database/sql package, the second using the pgx client package, and the third using the GORM ORM.

type Website struct {
    ID   int64
    Name string
    URL  string
    Rank int64
}
type Repository interface {
    Migrate(ctx context.Context) error
    Create(ctx context.Context, website Website) (*Website, error)
    All(ctx context.Context) ([]Website, error)
    GetByName(ctx context.Context, name string) (*Website, error)
    Update(ctx context.Context, id int64, updated Website) (*Website, error)
    Delete(ctx context.Context, id int64) error
}
5/21