How to Use GORM and Golang Migrate

Introduction This guide demonstrates how to set up and use GORM with Golang Migrate to manage your PostgreSQL database. Follow along to see the code in action and get your environment running. Prerequisites Go installed on your system. Docker installed and running. Project Structure Here is the final directory structure: . ├── .env ├── Makefile ├── cmd │ └── cli │ └── main.go ├── docker-compose.yml ├── go.mod ├── go.sum └── internal ├── db │ └── migrations │ ├── 20240608192206_create_users_table.down.sql │ └── 20240608192206_create_users_table.up.sql ├── models │ └── models.go └── repositories └── repositories.go 8 directories, 10 files Step 1: Initialize Your Project 1. Initialize the Go module: ...

June 7, 2024 · 4 min · 850 words · Hector Yeomans

Golang and interfaces misuse

One of my favorite things about Golang is the concept of interface. It’s also one of my grievances every time I see them used as C#/Java interfaces. It’s typical to see a colossal interface defined at a package level file, for example, a package that defines CRUD operations for a User. package db import "context" // User -- type User struct { ID int Email string } // UsersDb pointless interface type UsersDb interface { Get(ctx context.Context, id int) User Create(ctx context.Context, user User) User Update(ctx context.Context, id int, user User) User Delete(ctx context.Context, id int) bool } //... then we have the actual struct that "implements" the interface What benefit does this bring to the package? Some say mocking capabilities. From what I’ve seen to mock that kind of package, you need a generator that generates and updates the mocks. ...

January 18, 2020 · 3 min · 606 words · Hector Yeomans

Software as stitching

Photo by Alexander Andrews on Unsplash Lately, I’ve been thinking that most of the software I’ve done consist of stitching of API’s. Imagine you work on the team that is in charge of handling payments. It doesn’t matter if you’re working on the front-end or the back-end, it usually goes like this: We (the company you work for) have a new payment provider. We must integrate with their API. We must accommodate our business logic to their API. You have to take consideration of several layers in between, this list includes but is not limited to: validation, logging, error handling, persistence, a source of truth, actual business logic. Almost everything becomes a cross-cutting concern, authentication, logging, caching; however, in the end, the hardest part is making sense or making an abstraction of the new API to conform to the company business logic. ...

December 27, 2018 · 1 min · 211 words · Hector Yeomans

ES6 Arrow functions

Last night I was reading this post: ES6 arrow functions, syntax and lexical scoping and going through the comments I saw this question: so arrow functions always inherit scope? The answer was by Barney: always. I went to the console and typed: nvm use 4 node var doSome = () => { console.log(this.x) } doSome.call({x: 'hello'}); global.x = 'hello'; doSome.call({x: 'good bye'}); Could you guess what is going to be printed? I could replicate this on ES5 without fat arrow function. ...

September 21, 2015 · 1 min · 108 words · Hector Yeomans