Skip to main content

Category: Golang

Detecting MIME Types in Go

Introduction Knowing the type of a file you’re working with is not just a matter of curiosity — it’s often a necessity. This is especially true when you’re deciding whether or not a particular operation can be carried out on that file. Go, with its comprehensive standard library, offers a straightforward approach to identifying a file’s MIME type, ensuring that developers have the tools they need to make informed decisions about file manipulation.

Validating URLs with Go

Introduction In this post, we’ll take a quick look at URL validation using Golang. It’s common to implement URL validation as a task within a HTTP request pipeline, typically as middleware. There are many different definitions of “validation”. For the purpose of this article, we will simply validate that a URL conforms to a particular text pattern. I often see people (mistakenly) use URL and URI interchangeably. URL is actually is a sub-type of URI.

Golang: When Identical Strings are Not Equal

This will be a quick and dirty post, so please forgive any spelling/grammar mistakes. I was writing a little CLI tool in Golang to track todo items. Just a dumb little app to help hone my skills a bit, but still something useful that serves a purpose to me. I don’t write a ton of code at work (mostly just scripting/pipelines when I do), so I’m constantly working on something like this in my spare time.

Use “replace” in go.mod to Point to a Local Module

If you want to the local version of a dependency in Go rather than one in a remote repository, use the replace keyword. The replace line goes above your require statements, like so: module github.com/rnemeth90/foo replace github.com/rnemeth90/bar => /Users/rnemeth90/Projects/bar require ( github.com/rnemeth90/bar v1.0.0 ) Now when you compile this module go build or go install, it will use your local code rather than the remote dependency. According to the docs, you do need to make sure that the code you’re pointing to also has a go.

Reading Json Files with Go

JSON is a widely used format for representing structured data. Developers like it because it is easy to read, most common languages have a library for interacting with it, and most public APIs accept JSON in HTTP requests. In this post, we’ll look at parsing a JSON file using Go! We will be using the io/ioutil package to open a json file on local disk, and encoding/json to parse the JSON data within the file into a memory structure.