Jump to Content

Andrew M McCall

Go

Go Net Listener Http Server Example

We explore the net package, spin up a net.Listener, accept requests, and respond with html

Using Go Embed Package for Template Rendering

Go’s embed package is excellent for creating a Go template rendering engine. The benefit of using the embed package is that your templates get bundled with your binary. Learn how to use the embed package to render templates in golang applications.

Notes On Testing Golang Applications

Setup Main

setup_test.go is where you do any app setup for your test. This runs before the rest of the tests and can help you mock things that need to be mocked. When you build your application, test files are ignored. It is a safe space to declare variables, override variables, etc. Each package can have a TestMain func in the setup_test.go file.

// setup_test.go in the main package

package main

// may need to declare some testing specific vars up here

var mock *config.AppConfig
var mockDB *sqlDB

func TestMain(m *testing.M) {

    // configure your testing specific app config mocks

    mockApp.Title = ""
    mockApp.DSN = ""
    // mock session, repos, pass around any extra config to internal
    packages
    newSessionManager(mockApp, mockDB)
	handlers.SetHandlerRepo(handlers.NewHandlerRepo(mockApp, sqldbconn.NewSQLDbConn(mockApp, mockDB)))
	render.NewRenderer(mockApp)

    // the rest of the config setup that runs before test


    // init TestMain
    os.Exit(m.Run())



}

Adding Context And Session To http.Request

Create a couple of helper functions to create dummy session data and add it to the request:

Custom Golang Http Router With Middleware

Go standard library, as well as many others have already solved implementing a robust, production grade http server. So why bother writing one? This post will hopefully yield some insight of why it is important to revisit established technology.

Neovim Custom File Extension Syntax Highlighting

We learn how to use treesitter injections to setup syntax highlighting for custom file extensions

Golang Send Multipart Form Data to Api Endpoint

We often need to send data and files from one JSON API endpoint to another. In this post, I explain how to use multipartform writer to send form data across api endpoint boundaries.

Unit Testing Json Encode

Here are my notes on testing a JSON writer for Golang that lets you wrap the response in an envelope

Go Channels Tutorial - Understanding Goroutines and Channels

Go has powerful concurrency tools fully baked into the programming Language. This post aims to explain go routines and channels so you can get started using them in your own projects.

Golang Image Resize and Golang Image Crop: Image Manipulation With Go Programming Language

An introduction to some of the very basic features of iamge cropping with Go.

Golang Read Environment Variables From File: How To Do It [Code Snippets]

What Are Environmental Variables In Go

A Cautionary Tale: My AWS Account Was Hacked And I Have A $50,000 Bill

If you have ever used an API key or database connection string, you have most likely had a need to secure some sensitive data. Environment variables are a way to achieve this. We may need to do this because certain API’s can only be called so many times and the key is a way of authorizing our requests and keeping track of them. For example, the twitter API only allows 500k Tweets per monthcalls on the free Essential Access account per application. -Source: Twitter Developer Documentation