Gopher Coding (original) (raw)

<-- Goldie

Gopher Coding

Go, Docker, DevOps & Distributed Systems

Check If a URL is ValidCheck If a URL is ValidThis is just a quick post showing how we can check if a URL is a valid one. URLs are often passed from third parties or user input, so they need to be checked before used. This function allows you to do just that, it will return true if the string is a valid URL. Feel free to copy and paste it and go from there. e.g. valid “https://gophercoding.com/" e.g. invalid “test string”

#url#valid#function#check#checking

Get Unix Time in Go (Time Since Epoch)Get Unix Time in Go (Time Since Epoch)Unix time is the number of seconds since ‘Epoch’ (00:00:00 UTC on 1 January 1970). It’s a widely adopted way of representing time in a simple, uniform, manner - as it’s an integer and not a string. Because of it’s simplicity, it also used it many programming languages. In Go, you can easily access it from the time package by getting the current time, then calling the Unix() function.

#time#unix#current#format#seconds#now#january#epoch

Writing Tests in Go (a Beginners Guide)There a many benefits to testing your code in general, which we won’t go into detail on (but if you’re interested, take a look here) all we will say is the quote below. “Imperfect tests, run frequently, are much better than perfect tests that are never written at all”. - Fowler First Test We’ve created the most basic progam, to add two numbers together, below in main.go which we’re going to write a test for. Tests in Go are found in separate files to the code and are within *_test.go - where * is the filename. So our test would be main_test.go - but if your go file was address.go, you’d create a address_test.go and store them here.

#testing#unit#package#files#function#add

Check If a Date is in The Future?Check If a Date is in The Future?Whether you’re setting up a calendar event or issuing a JWT token, knowing how to manipulate and validate time can save you from many potential headaches. In Go, the built-in time package provides a comprehensive set of tools to work with dates and times. View Time Docs How to If you check the date in question, is after the current time then this basically does a ‘am I in the future’ check.

#date#future#compare#time#now#calendar

Generating UUIDs in GoUUIDs (Universally Unique Identifiers) serve as a standard for generating identifiers that are unique across time and space. In this blog post, we’ll explore how to generate UUIDs in Go using the google/uuid package. These can be especially useful when generating IDs across different systems - where it wouldn’t be possible to track using an auto incrementing integer.

#uuid#generate#package#id#identifier#string#random#universal

Should I Commit the go.sum File to the Repo?Should I Commit the go.sum File to the Repo?TL;DR Yes, probably. Using Go Modules, you will have both a go.mod and a go.sum file within your coding repository. A question often asked is whether you should commit the sum portion of the file as it’s automatically generated. These files help manage the dependencies of your project and they differ by the go.mod file being human-friendly, listing the libraries used within the project, and the go.sum listing the very specific vendor versions of each dependency (not just your direct ones, but also the whole tree).

#go#modules#merge#conflict#tldr#go.sum#go.mod#vendor#deploy

Find the Min or Max Value in a Slice (New to Go 1.21)Find the Min or Max Value in a Slice (New to Go 1.21)The latest Go release, 1.21, has introduced two new built-in functions: min() and max(). These functions have long been used in many other languages, such as Python and JavaScript, and are now welcome additions to Go. The min() and max() functions have been introduced to simplify the process of finding the smallest and largest numbers in a set, respectively. There was plenty of talk as to whether these should be included as “built-ins” or introduced into the cmp package.

#min#max#data#go1.21#sort#slice#generics#builtins#built-in#cmp

Recover from a PanicHow to catch a panic error when it’s thrown? That’s what this post hopes to answer. Go has a built in recover() function which allows you to pick up and run some code when a panic is thrown. This can be useful for regaining execution of your program, or allowing the panic to happen, but to clean up state (like files) before your program closes. If you are curious what the structure of a panic is, see it’s docs here.

#recover#panic#function#error-handling#defer#state#files

How to Ignore Weekends in GoGiven a date, we’re looking at how you can add a duration to it, like a month or two, but ignoring weekends. This is usually most used for businesses focused on working-days - but it could easily be adapted to do the opposite, only weekends. It’s built around the standard lib time package so no extra packages needed. We’ve got some example code below that makes a date and adds on 14 days, while skipping the weekend days out. We are doing this manually just checking each day on the Weekday() function and comparing it to time.Saturday or time.Sunday.

#weekend#ignore#skip#business#holiday#time#weekday#duration#month

Add Date Ordinals (UK Date Format) in GoAdd Date Ordinals (UK Date Format) in GoGo, despite its robust standard library, does not support ordinal dates out of the box like ‘th’ and ‘st’. This makes producing dates in the format “1st January 2000” much harder - which we use a lot here in the UK. In this post we’ll aim to show how you can make this easier. For reference, these are the options Go uses when chosing a new format: 1 Mon Jan 2 15:04:05 MST 2006 Our example below implements this for ourselves, as we create formatDateWithOrdinal() to print a given time in this format. This uses all three of the .Day() .Month() and .Year() functions within the time package, but passes the day through our extra function to add the ordinal.

#date#ordinal#format#uk#time#th

page += 1 page := 1