Go - Overview

Golang, or Go, is a statically typed, compiled programming language designed for simplicity and efficiency. It is commonly used for backend systems, cloud services, and DevOps tools due to its robust standard library and support for concurrent programming. Go follows a procedural and concurrent programming paradigm, making it ideal for modern computing challenges. Few things I like about go are The seperation of data (struct) and behavior (methods). This makes the code more readable and maintainable. Easiness to write concurrent code. Go routines and channels are very easy to use and understand. Zero values instead of null. This makes the code more predictable and less error prone. Few things I dislike about go are ...

Servers - Configuration Management Overview

There are different ways to manage configuration of a server. A common pattern is to use a combination of multiple sources, such as env variables, config files, cli args etc… and overlapping values are resolved based priority of the source. eg: configuring a server with a port number. cli args ( --port=8080 ) env variable ( port=8080 ) config file ( port: 8080 ) default value ( 8080 ) The configuration management system should look up the values and apply the value at the highest priority source. ...

Hugo - Using a hugo theme

Hugo is a great static site generator. The build times are pretty fast and there are theme which we can use. A lot of Hugo apis are available to setup a static site which is like a blog / personal website / documentation. Here are some cool themes Hugoplate Papermod Setting up hugo Initialize a new hugo site hugo new site mysite --format=yaml Add a theme ( using papermod as an example ) cd mysite git init git submodule add --depth=1 https://github.com/adityatelange/hugo-PaperMod.git themes/PaperMod Add the theme to the config file theme: "PaperMod" Adding content Any markdown files added in the folder content will be rendered as a page. Hugo follows route based on folder structure. ...

GO + HTMX - A Crud implementation

As I was exploring htmx, I wanted to try out an interactive workflow with it. A classic example is a data list, modal to create / update data and a delete. Setup with Gofiber Initialize go module and install dependencies go mod init htmx-crud go get github.com/gofiber/fiber/v2 go get github.com/gofiber/template/html/v2 Setup Server html template engine main.go package main import ( "log" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/log" "github.com/gofiber/template/html/v2" ) func main() { // Setup html template engine engine := html.New("./views", ".html") app := fiber.New(fiber.Config{ Views: engine, }) // Get request to render index page app.Get("/", func(c *fiber.Ctx) error { // Render index template return c.Render("index", nil, "layouts/main") }) // Start server log.Fatal(app.Listen(":3000")) } This is a simple server setup with gofiber. We are using html template engine to render the html. ...

Benchmarking GO vs Dotnet vs Nextjs

I was curious about how the server side rendering in terms of req/sec can go, dotnet and nextjs perform. So I created a simple server side rendering application in all three technologies and benchmarked them using wrk. Golang Golang was setup with gofiber and gotemplate. It performed well with around 14k req/sec. using minimal ram (~21mb) 10 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 34.05ms 31.00ms 119.93ms 77.11% Req/Sec 1.40k 411.25 4.61k 68.91% 419491 requests in 30.10s, 638.89MB read Socket errors: connect 0, read 413, write 0, timeout 0 Requests/sec: 13936.30 Transfer/sec: 21.23MB Dotnet Razor Dotnet razor performed well with around 11k req/sec. using a bit over idle ram (~53mb) ...

Razor Pages + HTMX - Handling form submissions.

Htmx allows us to handle async form submissions with ease by using hx-post attribute. In background it will issue a post request to the url specified in hx-post attribute and replace the element with the response. In this post we will try to handle form submissions in razor pages using htmx. Initialize a new razor pages project. dotnet new razor -o htmx-form In Pages/Index.cshtml we setup the layout. @page @using Htmx @model IndexModel @{ ViewData["Title"] = "Home page"; } <div class="row pb-5"> <div class="col"> Form here </div> <div class="col"> Login here </div> </div> Create a partial userform which will be used here and in the post request. ...

Standards - how they form, how I define standards for use cases

What is a standard? Standardization is a process of developing guidelines or way of doing things for common and repeated tasks. Standards are generaly opinionated by experts in the field and are widely accepted by the community. We require standards to follow to have a common ground for collaboration and organization of repeated tasks. Standards for collaboration When multiple people are working on same domain / system, it’s much easier to follow a pattern for specific tasks. Standards for organization ...

HTMX - Handling form submissions.

Htmx allows us to handle async form submissions with ease by using hx-post attribute. In background it will issue a post request to the url specified in hx-post attribute and replace the element with the response. Setup with Gofiber Initialize go module and install dependencies go mod init htmx-form go get github.com/gofiber/fiber/v2 go get github.com/gofiber/template/html/v2 Setup Server package main import ( "log" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/log" "github.com/gofiber/template/html/v2" ) func main() { // Setup html template engine engine := html.New("./views", ".html") app := fiber.New(fiber.Config{ Views: engine, }) // Get request to render index page app.Get("/", func(c *fiber.Ctx) error { values := fiber.Map{ "EmailErr": "", "PasswordErr": "", "Success": "", "Error": "", } // Render index template return c.Render("index", values) }) // Post request to handle form submission app.Post("/form", func(c *fiber.Ctx) error { // Get form values email := c.FormValue("email") password := c.FormValue("password") // Setup values to be passed to template values := fiber.Map{ "EmailValue": email, "EmailErr": "", "PasswordErr": "", "Success": "", "Error": "", } // Validate form values if email == "" { values["EmailErr"] = "Email is required" } if password == "" { values["PasswordErr"] = "Password is required" } if email != "" && password != "" { values["EmailValue"] = "" values["Success"] = "Validation success" } else { values["Error"] = "Validation error" } // Render index template return c.Render("form", values) }) log.Fatal(app.Listen(":3000")) } Setup Views ...

Nextjs - Setup with Bootstrap

Setup nextjs with bootstrap Initialize nextjs project with typescript pnpm create next-app --typescript Install bootstrap pnpm add bootstrap Add bootstrap css to app/layout.tsx or pages/_app import "bootstrap/dist/css/bootstrap.min.css"; Create a bootstrap client component components/Bootstrap.tsx "use client"; import { useEffect } from "react"; function BootstrapClient() { useEffect(() => { require("bootstrap/dist/js/bootstrap.bundle.min.js"); }, []); return null; } export default BootstrapClient; Add bootstrap client component to app/layout.tsx or pages/_app layout.tsx might look like this. import BootstrapClient from "@/components/boostrap"; import "bootstrap/dist/css/bootstrap.css"; import type { Metadata } from "next"; export const metadata: Metadata = { title: "Create Next App", description: "Generated by create next app", }; export default function RootLayout({ children, }: Readonly<{ children: React.ReactNode; }>) { return ( <html lang="en"> <body>{children}</body> <BootstrapClient /> </html> ); } Exporting as static site. Add the following to next.config.js ...

Go - Template

Let’s check the Go template syntax and essentials. Placeholders The {{ }} is used to denote a placeholder. It can contain a variable, function, control statments like if, for, range etc. a variant of placeholder is {{- -}} which is used to trim the whitespace before the placeholder, {{- trims the whitespace before the placeholder and -}} trims the whitespace after the placeholder. Variables Variables are defined using the {{ $variableName := value }} syntax. The variable can be used in the template using {{ $variableName }}. ...