Skip to main content

How to implement authentication and authorization in Golang

· One min read
Shekhar Patil
Full stack developer.

We planned to use following libraries:

  1. Gin: http web framwork go get -u github.com/gin-gonic/gin

  2. Gorm: ORM for relational database go get -u gorm.io/gorm

  3. go-jwt github.com/golang-jwt/jwt

  4. bycrypt:

  5. godotenv:

  6. Postgres database go get -u gorm.io/driver/postgres

Directory structure is as following

go-auth Project directory structure

models/user.go

package models

import "gorm.io/gorm"

type User struct {
gorm.Model

Name string `json:"name"`
Email string `json:"email"`
Password string `json:"password"`
Role string `json:"role"`
}

How to terminate goroutines in golang

· One min read
Shekhar Patil
Full stack developer.

How to terminate goroutines in golang?

Method 1: Using Channels to Signal Termination

package main

import (
"fmt"
"time"
)

func performTask(terminateChan <-chan bool) {
for {
select {
case <-terminateChan:
fmt.Println("Task terminated by channel")
return
default:
fmt.Println("Task is running")
time.Sleep(time.Second)
}
}
}

func main() {
terminateChan := make(chan bool)

go performTask(terminateChan)

time.Sleep(5 * time.Second)
terminateChan <- true
time.Sleep(time.Second)
fmt.Println("Main Goroutine exited")
}

Worker pool pattern in Golang

· 3 min read
Shekhar Patil
Full stack developer.

What is worker pool pattern?

The Worker Pool pattern is a concurrency pattern in Go that allows you to manage and control the number of goroutines working on a set of tasks. This pattern is particularly useful when you have a large number of tasks to process and want to limit the number of concurrent goroutines to prevent excessive resource usage.

Worker pool pattern