﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Home on Gopher Coding</title><link>https://gophercoding.com/</link><description>Recent content in Home on Gopher Coding</description><generator>Hugo</generator><language>en-gb</language><atom:link href="https://gophercoding.com/index.xml" rel="self" type="application/rss+xml"/><item><title>Check If a URL is Valid</title><link>https://gophercoding.com/is-url-valid/</link><pubDate>Mon, 04 Nov 2024 08:00:00 +0000</pubDate><guid>https://gophercoding.com/is-url-valid/</guid><description>&lt;p>This is just a quick post showing how we can check if a &lt;a href="https://en.wikipedia.org/wiki/URL">URL&lt;/a> is a valid one. URLs are often passed from &lt;strong>third parties&lt;/strong> or user input, so they need to be checked before used. This function allows you to do just that, it will return &lt;em>true&lt;/em> if the string is a valid URL. Feel free to copy and paste it and go from there.&lt;/p>
&lt;p>e.g. valid &amp;ldquo;&lt;a href="https://gophercoding.com/%22">https://gophercoding.com/&amp;quot;&lt;/a>&lt;/p>
&lt;p>e.g. invalid &amp;ldquo;test string&amp;rdquo;&lt;/p></description></item><item><title>Get Unix Time in Go (Time Since Epoch)</title><link>https://gophercoding.com/get-unix-time/</link><pubDate>Wed, 25 Sep 2024 05:00:00 +0000</pubDate><guid>https://gophercoding.com/get-unix-time/</guid><description>&lt;p>Unix time is the number of seconds since &amp;lsquo;&lt;a href="https://en.wikipedia.org/wiki/Epoch_(computing)">Epoch&lt;/a>&amp;rsquo; (00:00:00 UTC on 1 January 1970). It&amp;rsquo;s a widely adopted way of representing time in a simple, uniform, manner - as it&amp;rsquo;s an integer and not a string. Because of it&amp;rsquo;s simplicity, it also used it many programming languages.&lt;/p>
&lt;p>In Go, you can easily access it from the &lt;code>time&lt;/code> package by getting the current time, then calling the &lt;code>Unix()&lt;/code> function.&lt;/p></description></item><item><title>Writing Tests in Go (a Beginners Guide)</title><link>https://gophercoding.com/how-to-write-a-basic-test/</link><pubDate>Sat, 02 Mar 2024 05:00:00 +0000</pubDate><guid>https://gophercoding.com/how-to-write-a-basic-test/</guid><description>&lt;p>There a many benefits to testing your code in general, which we won&amp;rsquo;t go into detail on (but if you&amp;rsquo;re interested, take a look &lt;a href="https://stackoverflow.com/questions/67299/is-unit-testing-worth-the-effort">here&lt;/a>) all we will say is the quote below.&lt;/p>
&lt;blockquote>
&lt;p>&amp;ldquo;Imperfect tests, run frequently, are much better than perfect tests that are never written at all&amp;rdquo;. - Fowler&lt;/p>
&lt;/blockquote>
&lt;h2 id="first-test">First Test&lt;/h2>
&lt;p>We&amp;rsquo;ve created the most basic progam, to add two numbers together, below in &lt;code>main.go&lt;/code> which we&amp;rsquo;re going to write a test for. Tests in Go are found in &lt;strong>separate files&lt;/strong> to the code and are within &lt;code>*_test.go&lt;/code> - where * is the filename. So our test would be &lt;code>main_test.go&lt;/code> - but if your go file was &lt;code>address.go&lt;/code>, you&amp;rsquo;d create a &lt;code>address_test.go&lt;/code> and store them here.&lt;/p></description></item><item><title>Check If a Date is in The Future?</title><link>https://gophercoding.com/check-if-a-date-is-in-future/</link><pubDate>Mon, 13 Nov 2023 15:00:00 +0000</pubDate><guid>https://gophercoding.com/check-if-a-date-is-in-future/</guid><description>&lt;p>Whether you&amp;rsquo;re setting up a calendar event or issuing a JWT token, knowing how to manipulate and validate time can save you from many potential &lt;em>headaches&lt;/em>. In Go, the built-in &lt;code>time&lt;/code> package provides a comprehensive set of tools to work with dates and times.&lt;/p>
&lt;p>
&lt;a href="https://pkg.go.dev/time" class="cta-button" >View Time Docs&lt;/a>  &lt;/p>
&lt;h2 id="how-to">How to&lt;/h2>
&lt;p>If you check the date in question, is &lt;em>after&lt;/em> the current time then this basically does a &amp;lsquo;am I in the future&amp;rsquo; check.&lt;/p></description></item><item><title>Generating UUIDs in Go</title><link>https://gophercoding.com/generate-uuids-in-go/</link><pubDate>Sun, 12 Nov 2023 15:00:00 +0000</pubDate><guid>https://gophercoding.com/generate-uuids-in-go/</guid><description>&lt;p>UUIDs (&lt;a href="https://en.wikipedia.org/wiki/Universally_unique_identifier">Universally Unique Identifiers&lt;/a>) serve as a standard for generating identifiers that are &lt;strong>unique across time and space&lt;/strong>. In this blog post, we&amp;rsquo;ll explore how to generate UUIDs in Go using the &lt;a href="https://github.com/google/uuid">google/uuid&lt;/a> package. These can be especially useful when generating IDs across different systems - where it wouldn&amp;rsquo;t be possible to track using an auto incrementing integer.&lt;/p></description></item><item><title>Should I Commit the go.sum File to the Repo?</title><link>https://gophercoding.com/should-i-commit-my-go-sum/</link><pubDate>Tue, 31 Oct 2023 18:00:00 +0000</pubDate><guid>https://gophercoding.com/should-i-commit-my-go-sum/</guid><description>&lt;p>&lt;strong>TL;DR&lt;/strong> Yes, probably.&lt;/p>
&lt;p>Using &lt;a href="https://go.dev/blog/using-go-modules">Go Modules&lt;/a>, you will have both a &lt;code>go.mod&lt;/code> and a &lt;code>go.sum&lt;/code> file within your coding repository. A question often asked is whether you should commit the sum portion of the file as it&amp;rsquo;s &lt;strong>automatically generated&lt;/strong>. These files help manage the dependencies of your project and they differ by the &lt;code>go.mod&lt;/code> file being human-friendly, listing the libraries used within the project, and the &lt;code>go.sum&lt;/code> listing the very &lt;strong>specific vendor versions&lt;/strong> of each dependency (not just your direct ones, but also the whole tree).&lt;/p></description></item><item><title>Find the Min or Max Value in a Slice (New to Go 1.21)</title><link>https://gophercoding.com/using-min-max-on-slice/</link><pubDate>Sun, 23 Jul 2023 07:00:00 +0000</pubDate><guid>https://gophercoding.com/using-min-max-on-slice/</guid><description>&lt;p>The latest Go release, 1.21, has introduced two new built-in functions: &lt;code>min()&lt;/code> and &lt;code>max()&lt;/code>. These functions have long been used in many other languages, such as Python and JavaScript, and are now welcome additions to Go.&lt;/p>
&lt;p>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 &lt;a href="https://github.com/golang/go/issues/59488#issuecomment-1548505279">plenty of talk&lt;/a> as to whether these should be included as &amp;ldquo;built-ins&amp;rdquo; or introduced into the cmp package.&lt;/p></description></item><item><title>Recover from a Panic</title><link>https://gophercoding.com/recover-from-panic/</link><pubDate>Thu, 29 Jun 2023 14:00:00 +0000</pubDate><guid>https://gophercoding.com/recover-from-panic/</guid><description>&lt;p>How to catch a panic error when it&amp;rsquo;s thrown? That&amp;rsquo;s what this post hopes to answer.&lt;/p>
&lt;p>Go has a built in &lt;code>recover()&lt;/code> 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.&lt;/p>
&lt;p>If you are curious what the structure of a panic is, see it&amp;rsquo;s &lt;a href="https://go.dev/src/runtime/panic.go">docs here&lt;/a>.&lt;/p></description></item><item><title>How to Ignore Weekends in Go</title><link>https://gophercoding.com/ignore-weekends/</link><pubDate>Thu, 08 Jun 2023 14:00:00 +0000</pubDate><guid>https://gophercoding.com/ignore-weekends/</guid><description>&lt;p>Given a date, we&amp;rsquo;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&amp;rsquo;s built around the standard lib &lt;a href="https://pkg.go.dev/time">time&lt;/a> package so no extra packages needed.&lt;/p>
&lt;p>We&amp;rsquo;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 &lt;code>Weekday()&lt;/code> function and comparing it to &lt;code>time.Saturday&lt;/code> or &lt;code>time.Sunday&lt;/code>.&lt;/p></description></item><item><title>Add Date Ordinals (UK Date Format) in Go</title><link>https://gophercoding.com/add-ordinal-to-dates/</link><pubDate>Thu, 01 Jun 2023 19:00:00 +0000</pubDate><guid>https://gophercoding.com/add-ordinal-to-dates/</guid><description>&lt;p>Go, despite its robust standard library, does &lt;em>not&lt;/em> support &lt;a href="https://en.wikipedia.org/wiki/Ordinal_date">ordinal dates&lt;/a> out of the box like &amp;lsquo;&lt;strong>th&lt;/strong>&amp;rsquo; and &amp;lsquo;&lt;strong>st&lt;/strong>&amp;rsquo;. This makes producing dates in the format &amp;ldquo;1st January 2000&amp;rdquo; much harder - which we use a lot here in the UK. In this post we&amp;rsquo;ll aim to show how you can make this easier.&lt;/p>
&lt;p>For reference, these are the options Go uses when chosing a new format:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-txt" data-lang="txt">&lt;span class="line">&lt;span class="cl">Mon Jan 2 15:04:05 MST 2006
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>Our example below implements this for ourselves, as we create &lt;code>formatDateWithOrdinal()&lt;/code> to print a given time in this format. This uses all three of the .Day() .Month() and .Year() functions within the &lt;a href="https://pkg.go.dev/time">time&lt;/a> package, but passes the day through our extra function to add the ordinal.&lt;/p></description></item><item><title>Redis: Connect, Set &amp; Get in Go</title><link>https://gophercoding.com/redis-connection/</link><pubDate>Thu, 04 May 2023 16:00:00 +0000</pubDate><guid>https://gophercoding.com/redis-connection/</guid><description>&lt;p>Redis is a in-memory data store, used a lot in the backend systems of web-apps. Specifically, it&amp;rsquo;s good at helping with caching and message as it&amp;rsquo;s high performance lends itself to fast response times at retriving data.&lt;/p>
&lt;p>In this blog post, we will demonstrate how to connect to Redis using Go and the go-redis library, which is one of the most popular and widely-used libraries for interacting with Redis in Go.&lt;/p></description></item><item><title>Search &amp; Replace All in Strings</title><link>https://gophercoding.com/search-replace-in-strings/</link><pubDate>Mon, 01 May 2023 16:00:00 +0000</pubDate><guid>https://gophercoding.com/search-replace-in-strings/</guid><description>&lt;p>One common task when working with text in code, is searching and replacing inside strings. For example you might want to replace X with Y. Or equally, you might want to replace Z with nothing, removing it. In this blog post, we&amp;rsquo;ll explore &lt;em>two ways&lt;/em> to perform a search and replace operation in Go using the &lt;strong>strings&lt;/strong> and &lt;strong>regexp&lt;/strong> packages.&lt;/p>
&lt;h2 id="strings-package">Strings Package&lt;/h2>
&lt;p>The strings package provides a simple method called Replace() for search and replace operations. This method has the following signature:&lt;/p></description></item><item><title>Organizing Structs for Memory Efficiency</title><link>https://gophercoding.com/organizing-structs/</link><pubDate>Sun, 30 Apr 2023 06:00:00 +0000</pubDate><guid>https://gophercoding.com/organizing-structs/</guid><description>&lt;p>Go is generally a very memory efficient language to work in, but knowing this little technique can make it that bit &lt;em>more efficient&lt;/em> again.&lt;/p>
&lt;p>First, we&amp;rsquo;ll look at structs - they are a composite data type used to group together zero or more values, each with its own name and type, under a single name. They are the foundation for building complex data structures and objects.&lt;/p>
&lt;!-- raw HTML omitted -->
&lt;!-- raw HTML omitted -->
&lt;p>Memory alignment is an essential aspect to consider when organizing structs in Go. The padding introduced due to alignment can lead to memory waste. To reduce padding and optimize memory usage, follow these guidelines:&lt;/p></description></item><item><title>Reduce Go Binary Size</title><link>https://gophercoding.com/reduce-go-binary-size/</link><pubDate>Thu, 06 Apr 2023 18:00:00 +0000</pubDate><guid>https://gophercoding.com/reduce-go-binary-size/</guid><description>&lt;p>Always a &lt;a href="https://github.com/xaionaro/documentation/blob/master/golang/reduce-binary-size.md">hot&lt;/a>-&lt;a href="https://github.com/golang/go/issues/6853">topic&lt;/a> in Go circles is binary size of applications. In this post we will show you how you can reduce the binary size produced from &lt;code>go build&lt;/code> for leaner, slimer builds.&lt;/p>
&lt;p>In short, adding the &lt;strong>ldflags&lt;/strong> shown below will reduce the size of the binary produced. It does this by removing some perhaps non-critial parts (shouldn&amp;rsquo;t affect execution, only debugging potentially).&lt;/p>
&lt;p>&lt;strong>&lt;code>-s&lt;/code>&lt;/strong> Will turn off the symbol table, so you won&amp;rsquo;t be able to use commands like &lt;code>go tool nm&lt;/code>.&lt;/p></description></item><item><title>Using Iota for a List of Constants (Like Enums)</title><link>https://gophercoding.com/using-iota-for-lists-of-contants-like-enums/</link><pubDate>Tue, 04 Apr 2023 16:50:00 +0000</pubDate><guid>https://gophercoding.com/using-iota-for-lists-of-contants-like-enums/</guid><description>&lt;p>Enums have been used in &lt;a href="https://softwareengineering.stackexchange.com/questions/299268/what-was-the-first-programming-language-with-enumerations">many programming languages&lt;/a> in varying forms for many years. But if you&amp;rsquo;ve found this post, then you are most likely looking for how to do this in Go (golang). In Go, there&amp;rsquo;s a handy feature called &lt;strong>iota&lt;/strong> which is a running list of ever-increasing constants. They are a consise way of presenting a list of things in a &amp;ldquo;&lt;em>don&amp;rsquo;t repeat yourself&lt;/em>&amp;rdquo; way.&lt;/p>
&lt;p>For example if you need to define days of the week using contants, you could:&lt;/p></description></item><item><title>Run a SQL Query Through SSH</title><link>https://gophercoding.com/sql-query-through-ssh/</link><pubDate>Thu, 23 Mar 2023 18:00:00 +0000</pubDate><guid>https://gophercoding.com/sql-query-through-ssh/</guid><description>&lt;p>This post shows how you can &lt;strong>connect to a database remotely via a &lt;a href="https://en.wikipedia.org/wiki/Secure_Shell">SSH&lt;/a> connection&lt;/strong>. In our example we&amp;rsquo;ll be connecting to a &lt;em>MySQL&lt;/em> or &lt;em>MariaDB&lt;/em> database, but the same method will apply to many other SQL databases like &lt;em>PostgreSQL&lt;/em>. This technique is especially useful if the database isn&amp;rsquo;t accessible due to &lt;strong>firewall&lt;/strong> rules - for example on a web server. But if you have SSH access, it&amp;rsquo;s just like logging in and running the command yourself.&lt;/p></description></item><item><title>A Basic Web Scraper in Go</title><link>https://gophercoding.com/web-scraper/</link><pubDate>Sun, 19 Mar 2023 09:30:00 +0000</pubDate><guid>https://gophercoding.com/web-scraper/</guid><description>&lt;p>A basic web scraper usually involves a few steps, fetching the content, querying it for the data you&amp;rsquo;re after and then sometimes, using that data to go and find more as a loop.&lt;/p>
&lt;p>In our example below, we use a package called &lt;code>goquery&lt;/code> to do most of the heavy lifting for us. This library will go further than Go&amp;rsquo;s standard library by allowing us to do both the first and second steps a bit easier. It uses &lt;code>NewDocument()&lt;/code> to get the content, then &lt;code>Find()&lt;/code> to query it.&lt;/p></description></item><item><title>How to Format Your Golang Code</title><link>https://gophercoding.com/how-to-format-golang-code/</link><pubDate>Mon, 27 Feb 2023 09:30:00 +0000</pubDate><guid>https://gophercoding.com/how-to-format-golang-code/</guid><description>&lt;p>Whether you agree with it or not, Go defines a &lt;a href="https://google.github.io/styleguide/go/guide">code style&lt;/a> at a &lt;strong>language level&lt;/strong> (which I love), and not different styles per project - or having the style as an afterthought! This means there&amp;rsquo;s very little opinion or conflict when moving between projects so it&amp;rsquo;s easy. This is very important to Go, as it stresses &lt;strong>&lt;a href="https://google.github.io/styleguide/go/guide.html">clarity and consistency&lt;/a>&lt;/strong> as some of the most important factors of code style.&lt;/p>
&lt;p>So you&amp;rsquo;ve written some code, and have been told to format it?&lt;/p></description></item><item><title>SHA3 Hash in Golang (256-bit)</title><link>https://gophercoding.com/sha3-encode-in-golang/</link><pubDate>Sun, 12 Feb 2023 09:30:00 +0000</pubDate><guid>https://gophercoding.com/sha3-encode-in-golang/</guid><description>&lt;p>You can use the &lt;code>golang.org/x/crypto/sha3&lt;/code> [&lt;a href="golang.org/x/crypto/sha3">docs&lt;/a>] package to perform SHA-3 encoding on a string. We have an example function below, it will take your string and write it as a hash. Then convert that binary back into a hexadecimal string using &lt;code>Sprintf&lt;/code>.&lt;/p>
&lt;h2 id="advantages-of-using-sha3">Advantages of Using SHA3&lt;/h2>
&lt;ol>
&lt;li>
&lt;p>&lt;strong>Security&lt;/strong>: SHA-3 was designed to provide higher security compared to SHA-2 by using a different construction called &amp;ldquo;sponge construction&amp;rdquo;, which makes it more resistant to various types of attacks, such as collision and preimage attacks.&lt;/p></description></item><item><title>MD5 Encoding in Golang</title><link>https://gophercoding.com/md5-encode-in-golang/</link><pubDate>Sun, 12 Feb 2023 09:00:00 +0000</pubDate><guid>https://gophercoding.com/md5-encode-in-golang/</guid><description>&lt;p>You can use the &lt;code>crypto/md5&lt;/code> [&lt;a href="https://pkg.go.dev/crypto/md5">docs&lt;/a>] package in Go to perform MD5 encoding on a string. We have an example function below, it will take your string and write it as a hash. Then convert that binary back into a hexadecimal string using &lt;code>Sprintf&lt;/code>.&lt;/p>
&lt;p>&lt;strong>Note:&lt;/strong> There are &lt;a href="https://security.stackexchange.com/questions/19906/is-md5-considered-insecure">more modern approaches&lt;/a> than md5 these days - and it isn&amp;rsquo;t recommended for many things, but definitely not password hashing.&lt;/p>
&lt;p>Here&amp;rsquo;s an example of how to use it:&lt;/p></description></item><item><title>How to Write Data to CSV in Go</title><link>https://gophercoding.com/saving-data-to-csv/</link><pubDate>Sat, 11 Feb 2023 13:00:00 +0000</pubDate><guid>https://gophercoding.com/saving-data-to-csv/</guid><description>&lt;p>In Go, you can use the standard library package &lt;code>encoding/csv&lt;/code> [&lt;a href="https://pkg.go.dev/encoding/csv">docs&lt;/a>] to write data to a CSV file. Below is a example that shows you how you can write a slice of user-data related strings to a CSV file.&lt;/p>
&lt;p>The code creates a new file called &lt;code>users.csv&lt;/code> and writes a slice of records to it. Finally, the Flush method is used to flush any buffered data to the underlying io.Writer, which is the file.&lt;/p></description></item><item><title>Top Golang Books for Learning in 2023</title><link>https://gophercoding.com/best-golang-books-for-learning/</link><pubDate>Sat, 11 Feb 2023 13:00:00 +0000</pubDate><guid>https://gophercoding.com/best-golang-books-for-learning/</guid><description>&lt;h2 id="1-go-programming-language">1. Go Programming Language&lt;/h2>
&lt;blockquote>
&lt;p>Google’s Go team member Alan A. A. Donovan and Brian Kernighan, co-author of The C Programming Language, provide hundreds of interesting and practical examples of well-written Go code to help programmers learn this flexible, and fast, language.&lt;/p>
&lt;/blockquote>


&lt;a href="https://www.amazon.co.uk/Programming-Language-Addison-Wesley-Professional-Computing/dp/0134190440?crid=1PX0BDLVNOF9P&amp;keywords=go+programming&amp;qid=1674597085&amp;sprefix=go+programming%2Caps%2C90&amp;sr=8-3&amp;linkCode=li3&amp;tag=fzcv3424be-21&amp;linkId=5600629dafa707c25a0d07b4d3ef52b8&amp;language=en_GB&amp;ref_=as_li_ss_il" target="_blank">&lt;img border="0" src="//ws-eu.amazon-adsystem.com/widgets/q?_encoding=UTF8&amp;ASIN=0134190440&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=GB&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=fzcv3424be-21&amp;language=en_GB" >&lt;/a>&lt;img src="https://ir-uk.amazon-adsystem.com/e/ir?t=fzcv3424be-21&amp;language=en_GB&amp;l=li3&amp;o=2&amp;a=0134190440" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />

&lt;p>
&lt;a href="https://www.amazon.co.uk/Programming-Language-Addison-Wesley-Professional-Computing/dp/0134190440?crid=1PX0BDLVNOF9P&amp;amp;keywords=go&amp;#43;programming&amp;amp;qid=1674597085&amp;amp;sprefix=go&amp;#43;programming%2Caps%2C90&amp;amp;sr=8-3&amp;amp;linkCode=ll1&amp;amp;tag=fzcv3424be-21&amp;amp;linkId=4d82577d30cb82e8819be90103277df0&amp;amp;language=en_GB&amp;amp;ref_=as_li_ss_tl" class="cta-button" rel="noopener" target="__target">View on Amazon&lt;/a>  &lt;/p>
&lt;h2 id="2-learning-go-an-idiomatic-approach-to-real-world-go-programming">2. Learning Go: An Idiomatic Approach to Real-World Go Programming&lt;/h2>
&lt;blockquote>
&lt;p>Go is rapidly becoming the preferred language for building web services. While there are plenty of tutorials available that teach Go&amp;rsquo;s syntax to developers with experience in other programming languages, tutorials aren&amp;rsquo;t enough.&lt;/p></description></item><item><title>Deploy a Go App to Railway</title><link>https://gophercoding.com/deploy-go-app-to-railway/</link><pubDate>Tue, 17 Jan 2023 15:00:00 +0000</pubDate><guid>https://gophercoding.com/deploy-go-app-to-railway/</guid><description>&lt;p>After using Heroku for many years, I&amp;rsquo;ve recently taken a look into &lt;a href="https://railway.app?referralCode=3dqien">Railway&lt;/a> as an alternative hosting platform for various side projects. These projects are often written in Go (of course!) so I thought I&amp;rsquo;d write a quick guide on how to setup a new project - the good news is, it&amp;rsquo;s easy!&lt;/p>
&lt;h2 id="1-get-an-account">1) Get an Account&lt;/h2>
&lt;p>It&amp;rsquo;s easy to setup, the link on their home page will help you.&lt;/p>
&lt;p>
&lt;a href="https://railway.app?referralCode=3dqien" class="cta-button" >Signup to Railway&lt;/a>  &lt;/p></description></item><item><title>Add Swagger Docs to a Go API</title><link>https://gophercoding.com/add-swagger-docs-to-go-api/</link><pubDate>Fri, 23 Dec 2022 17:00:00 +0000</pubDate><guid>https://gophercoding.com/add-swagger-docs-to-go-api/</guid><description>&lt;p>&lt;a href="https://en.wikipedia.org/wiki/Swagger_(software)">Swagger&lt;/a> allows developers to easily create api documentation which is defined using comments in the code and then generated as a yaml config file. In this post we&amp;rsquo;ll be using it alongside Go (golang).&lt;/p>
&lt;p>Using tools like Swagger has many &lt;em>advantages&lt;/em>, like allowing you to &lt;strong>generate documentation automatically&lt;/strong> (saving you time) and that it keeps &lt;strong>your code and documentation as close as possible&lt;/strong> (in distance terms). The idea being that if the docs are hard to update and far away, you just won&amp;rsquo;t.&lt;/p></description></item><item><title>Get/Set HTTP Headers in Go Request</title><link>https://gophercoding.com/get-set-http-headers-in-go-request/</link><pubDate>Wed, 21 Dec 2022 17:00:00 +0000</pubDate><guid>https://gophercoding.com/get-set-http-headers-in-go-request/</guid><description>&lt;p>HTTP headers, we all need &amp;rsquo;em &amp;#x1f609;&lt;/p>
&lt;p>Here&amp;rsquo;s how you can &lt;strong>get&lt;/strong> and &lt;strong>set&lt;/strong> these headers within your Go requests. These include request coming &lt;em>into&lt;/em> your router handlers and requests you are sending out to other systems through &lt;code>net/http&lt;/code>. This can be thought of as being the same as reading headers from a request and creating new ones.&lt;/p>
&lt;p>First we&amp;rsquo;ll start with reading them from the request.&lt;/p></description></item><item><title>Print Current Memory Usage</title><link>https://gophercoding.com/print-current-memory-usage/</link><pubDate>Mon, 05 Dec 2022 17:00:00 +0000</pubDate><guid>https://gophercoding.com/print-current-memory-usage/</guid><description>&lt;p>In this post we show how you can print memory usage in golang. We do this by outputting the current &lt;strong>state&lt;/strong> of memory at any given time to show how much ram has been allocated and gc cycles made. We have created a function &lt;code>PrintMemUsage()&lt;/code> to help out, so you can call this when ever you need to know.&lt;/p>
&lt;p>All the info we need can be acquired through the &lt;code>runtime&lt;/code> [&lt;a href="https://pkg.go.dev/runtime">docs&lt;/a>] package, which allows us to read the state of the memory into the &lt;code>MemStats&lt;/code> struct. It returns stats like how much memory the program is using, how much of it the OS has allocated to it and the number of &lt;em>garbage collections&lt;/em>.&lt;/p></description></item><item><title>Change Go Version in Mod</title><link>https://gophercoding.com/change-go-version-in-mod/</link><pubDate>Sat, 26 Nov 2022 07:00:00 +0000</pubDate><guid>https://gophercoding.com/change-go-version-in-mod/</guid><description>&lt;p>Picking up an old Go project, we wanted to update the desired version it should run on (mainly so when we deployed to our &lt;em>live&lt;/em> systems it would use this version too). To do this, we updated the version within the &lt;code>go.mod&lt;/code> file - the file which keeps track of both versions and packages used by the project.&lt;/p>
&lt;p>We used the command below:&lt;/p></description></item><item><title>How to Die Dump in Go - dd()</title><link>https://gophercoding.com/how-to-die-dump-in-go/</link><pubDate>Sun, 06 Nov 2022 07:00:00 +0000</pubDate><guid>https://gophercoding.com/how-to-die-dump-in-go/</guid><description>&lt;p>Having come from the PHP community, we often have a handy debug function at our disposal &lt;code>dd()&lt;/code> (part of Laravel) and &lt;code>var_dump()&lt;/code> (native). This may not be perfect (compared with a full on debugging suite) but it is a quick and easy debugging method. This post gives an idea on how you can do the same, but in Go.&lt;/p>
&lt;p>We&amp;rsquo;re helped out by being able to use both variable parameters (shown with &lt;code>...&lt;/code>) and the interface type to handle different types, both strings and integers in our examples.&lt;/p></description></item><item><title>Get Status Code from HTTP Request</title><link>https://gophercoding.com/get-status-code-from-http-req/</link><pubDate>Sun, 23 Oct 2022 07:00:00 +0000</pubDate><guid>https://gophercoding.com/get-status-code-from-http-req/</guid><description>&lt;p>We won&amp;rsquo;t go into too much detail about &lt;a href="https://en.wikipedia.org/wiki/List_of_HTTP_status_codes">HTTP status codes&lt;/a> themselves, but in this post we will talk about how to use the &lt;strong>status code&lt;/strong> after making a request, how to check them as a range and how to print them as text. This is often important so we can check if something was successful or failed.&lt;/p>
&lt;p>You can always get this data if you have a &lt;code>net/http/Response&lt;/code> type (&lt;a href="https://go.dev/src/net/http/response.go">spec&lt;/a>).&lt;/p></description></item><item><title>Convert an io.ReadCloser to String</title><link>https://gophercoding.com/convert-readcloser-to-string/</link><pubDate>Sat, 01 Oct 2022 07:00:00 +0000</pubDate><guid>https://gophercoding.com/convert-readcloser-to-string/</guid><description>&lt;p>Using Go, we often make HTTP calls these days using &lt;code>net/http&lt;/code>, which result in a response of type &lt;code>io.ReadCloser&lt;/code>&amp;hellip; which are hard to read for the layman (like me). What we really want to the response in the form of &lt;strong>a string which we can read&lt;/strong>. This post will talk about how to convert these &lt;strong>ReadCloser&lt;/strong> into strings.&lt;/p>
&lt;p>First we&amp;rsquo;ll look at the problem, then we have two different solutions.&lt;/p></description></item><item><title>Example Netlify Function in Go</title><link>https://gophercoding.com/example-netlify-function-in-go/</link><pubDate>Sun, 25 Sep 2022 07:00:00 +0000</pubDate><guid>https://gophercoding.com/example-netlify-function-in-go/</guid><description>&lt;p>&lt;strong>Netlify&lt;/strong>, the hosting and web platform, allows you to create &lt;a href="https://www.netlify.com/products/functions/">&amp;quot;&lt;em>functions&lt;/em>&amp;quot;&lt;/a> along with their CDN. These functions are hosted on AWS&amp;rsquo; Lambda and can be accessible via a URL. So you can create static sites, with extra ability and dynamism (like we use on this site).&lt;/p>
&lt;p>We wanted to share a post giving an example how to write one of these functions in Go. The aim of the code (below) is to return the version of golang.&lt;/p></description></item><item><title>Remove All Non-Alphanumeric Characters</title><link>https://gophercoding.com/remove-non-alphanumeric-chars/</link><pubDate>Fri, 23 Sep 2022 18:00:00 +0000</pubDate><guid>https://gophercoding.com/remove-non-alphanumeric-chars/</guid><description>&lt;p>We often need to remove symbols and special characters from the strings we&amp;rsquo;re using (&lt;em>especially with currency!&lt;/em>). This post shows how you can keep the letters and numbers, but remove any punctuation, symbols, grammar, etc. For example, if a user types in &amp;ldquo;$1,000&amp;rdquo; you can turn it into &amp;ldquo;1000&amp;rdquo;.&lt;/p>
&lt;p>We use the &lt;code>regexp&lt;/code> package to do this, first building a regex with &lt;code>.Compile()&lt;/code> then running the string through that regex with &lt;code>.ReplaceAllString()&lt;/code>. Finally with display and compare both strings.&lt;/p></description></item><item><title>Download a File from a URL</title><link>https://gophercoding.com/download-a-file/</link><pubDate>Sun, 18 Sep 2022 08:00:00 +0000</pubDate><guid>https://gophercoding.com/download-a-file/</guid><description>&lt;p>This post shows how you can &lt;strong>download a file&lt;/strong> in Go from a URL. We use the std lib (standard library) &lt;code>http.Get()&lt;/code> [&lt;a href="https://pkg.go.dev/net/http#Client.Get">docs&lt;/a>] and &lt;code>io.Copy()&lt;/code> [&lt;a href="https://pkg.go.dev/io#Copy">docs&lt;/a>] functions to help us with this. This function should be &lt;em>efficient&lt;/em> as it will stream the data into the file, as opposed to downloading it all into memory, then to file.&lt;/p>
&lt;p>The file will be saved in the same directory as your program.&lt;/p>
&lt;p>We also show &lt;a href="#alternative-filename">an alternative&lt;/a> below if you want to take the filename from the URL.&lt;/p></description></item><item><title>Convert HEIC to JPG in Go</title><link>https://gophercoding.com/convert-heic-to-jpeg-go/</link><pubDate>Sat, 17 Sep 2022 12:00:00 +0000</pubDate><guid>https://gophercoding.com/convert-heic-to-jpeg-go/</guid><description>&lt;p>The &amp;ldquo;High Efficiency Image File Format&amp;rdquo; or &lt;strong>HEIF&lt;/strong> is an image format often used by Apple devices. Although called HEIF, the file types are often &lt;strong>heic&lt;/strong> (&lt;em>presumably the &amp;lsquo;c&amp;rsquo; stands for container?&lt;/em>) They use a similar encoding method of video formats and are deemed better quality and lower file size than standard jpeg files.&lt;/p>
&lt;p>In our example, we&amp;rsquo;re trying to convert these &lt;code>heic&lt;/code> files back into &lt;code>jpg&lt;/code> files though - so we can use them elsewhere, display them or what ever we choose.&lt;/p></description></item><item><title>Find the Length of an Array</title><link>https://gophercoding.com/find-length-of-array/</link><pubDate>Thu, 15 Sep 2022 15:00:00 +0000</pubDate><guid>https://gophercoding.com/find-length-of-array/</guid><description>&lt;p>You can find the length of an &lt;strong>array&lt;/strong>, or to be correct a &lt;em>slice&lt;/em> or &lt;em>map&lt;/em>, in Go by using the standard library function &lt;code>len()&lt;/code>. We use the term array loosely here, as a general variable holding multiple things. Maps tend to have defined keys, whereas slices don&amp;rsquo;t (&lt;a href="https://boltandnuts.wordpress.com/2017/11/20/go-slice-vs-maps/">more info on the difference here&lt;/a>).&lt;/p>
&lt;p>We have shown both as examples below, how to get the length of a slice and how to get the length of a map. In Golang, the method is the same for both.&lt;/p></description></item><item><title>Sleeping in Golang</title><link>https://gophercoding.com/sleeping-in-golang/</link><pubDate>Thu, 08 Sep 2022 19:00:00 +0000</pubDate><guid>https://gophercoding.com/sleeping-in-golang/</guid><description>&lt;p>Sleeping in Go and how to pause execution and sleep for a number of seconds in Go (golang). We can use the &lt;a href="https://pkg.go.dev/time">time&lt;/a> package from the standard library to sleep for a duration of time. You can use any duration of time, providing you use the constants provided.&lt;/p>
&lt;p>In our example below we sleep for two seconds, and to illustrate the point, we print out the time before and after we do this.&lt;/p></description></item><item><title>Convert String to Int, Int64, Uint64</title><link>https://gophercoding.com/convert-string-to-int/</link><pubDate>Thu, 25 Aug 2022 20:00:00 +0000</pubDate><guid>https://gophercoding.com/convert-string-to-int/</guid><description>&lt;p>This is one of those posts that won&amp;rsquo;t need a huge introduction. We want to convert a string into an integer. We&amp;rsquo;ll look at the different ways to do this (based on which sort of int you want). We&amp;rsquo;ll convert to &lt;code>int&lt;/code>, &lt;code>uint&lt;/code> and &lt;code>uint64&lt;/code> (though a int64 will be easy enough work out).&lt;/p></description></item><item><title>Run Golang Tests with Github Actions</title><link>https://gophercoding.com/run-go-tests-with-github-actions/</link><pubDate>Tue, 21 Jun 2022 18:30:00 +0000</pubDate><guid>https://gophercoding.com/run-go-tests-with-github-actions/</guid><description>&lt;p>Automation is great (&lt;em>right?!&lt;/em>) and it can be so easy these days to run your tests automatically on any new pull request or code change. In this post we outline how you can automatically run your &lt;strong>Go&lt;/strong> (golang) test for your project by using &lt;strong>Github Actions&lt;/strong>. The results will then show within Github and is configured using a &lt;strong>yaml&lt;/strong> file.&lt;/p>
&lt;p>To get this running, you will need to create a workflow file (as shown below). Which will essentially do the following:&lt;/p></description></item><item><title>Docker Setup for Go Development</title><link>https://gophercoding.com/docker-setup-for-go-development/</link><pubDate>Sat, 18 Jun 2022 15:00:00 +0000</pubDate><guid>https://gophercoding.com/docker-setup-for-go-development/</guid><description>&lt;p>This is a short and sweet article on running a Go application using Docker. This is a generic example and many use-cases will differ and so have totally different setups. In our examples we&amp;rsquo;re also using &lt;strong>Docker&lt;/strong> and &lt;strong>docker-compose&lt;/strong> both of which you will need already installed and setup on your machine for this to work.&lt;/p></description></item><item><title>Update All Go Packages in Your Project</title><link>https://gophercoding.com/update-all-packages-dependencies/</link><pubDate>Sat, 18 Jun 2022 14:00:00 +0000</pubDate><guid>https://gophercoding.com/update-all-packages-dependencies/</guid><description>&lt;p>If you want to update all dependencies and &lt;strong>packages&lt;/strong> within your project then the simplest way is to use the &lt;code>go get&lt;/code> command which handles it all for us. Go modules (&lt;strong>go mod&lt;/strong>) will handle most of the versioning of these packages, so deploying and sharing the project will keep the same package versions.&lt;/p></description></item><item><title>Remove Old Docker Containers (Spring Cleaning)</title><link>https://gophercoding.com/clear-out-docker-containers/</link><pubDate>Tue, 14 Jun 2022 20:00:00 +0000</pubDate><guid>https://gophercoding.com/clear-out-docker-containers/</guid><description>&lt;p>If you just dabble with &lt;strong>Docker&lt;/strong>, or use it regularly, you will build an arsenal of images and containers as you go along. From time-to-time we need to clear these out. This process used to be more complicated until the &lt;code>docker&lt;/code> command implemented a simpler way using system prune.&lt;/p></description></item><item><title>Check If a Go Client Has Reused a Connection</title><link>https://gophercoding.com/has-client-reused-connection/</link><pubDate>Sat, 11 Jun 2022 13:03:00 +0000</pubDate><guid>https://gophercoding.com/has-client-reused-connection/</guid><description>&lt;p>For logging purposes, performance monitoring, debugging - what ever your reason, it can be useful to know if Go has reused it&amp;rsquo;s connection when making an initial request, for later use. If it&amp;rsquo;s not reusing connections, it might be running slower and less efficiently than it needs to be.&lt;/p>
&lt;p>The code below is used as an example of how to log if connections are being reused, as well as outputting the DNS information gathered.&lt;/p></description></item><item><title>Advantages &amp; Disadvantages of Microservices</title><link>https://gophercoding.com/advantages-disadvantages-microservices/</link><pubDate>Sun, 05 Jun 2022 08:03:00 +0000</pubDate><guid>https://gophercoding.com/advantages-disadvantages-microservices/</guid><description>&lt;p>Microservices are a form of &lt;a href="https://en.wikipedia.org/wiki/Microservices">service-orientated architecture&lt;/a> - we won&amp;rsquo;t go into too much detail on what they are, but we will discuss their pro&amp;rsquo;s and con&amp;rsquo;s here. As with all architectures, there is no right way that everyone should use so discussing their plus points with the potential issues is important.&lt;/p>
&lt;h2 id="advantages">Advantages&lt;/h2>
&lt;p>First a quick look at why you should use them.&lt;/p>
&lt;h3 id="independent-technology">Independent Technology&lt;/h3>
&lt;p>Using microservices to split up your infrastructure allows you to use different technology/programming languages/tools very easily. More than this, it means the languages that you do use for a single service are often easier to update and maintain as the list of dependencies and legacy code attached to them is less and with a large monolith system.&lt;/p></description></item><item><title>Install Latest Version of Go (Linux/OSX)</title><link>https://gophercoding.com/install-latest-version-of-go/</link><pubDate>Sat, 04 Jun 2022 09:03:00 +0000</pubDate><guid>https://gophercoding.com/install-latest-version-of-go/</guid><description>&lt;p>This article describes how to install the latest version of Go (golang). It also helpfully updates itself by pulling the latest version numbers directly, so you don&amp;rsquo;t have to go and dig out the latest version each time they release a version (yay!) - just copy and paste away. For more info, try their &lt;a href="https://go.dev/doc/install">installer docs&lt;/a>.&lt;/p></description></item><item><title>Using a .env File &amp; Environment Variables</title><link>https://gophercoding.com/dot-env-get-environment-vars/</link><pubDate>Fri, 03 Jun 2022 09:02:00 +0000</pubDate><guid>https://gophercoding.com/dot-env-get-environment-vars/</guid><description>&lt;p>Environment variables are used throughout the coding ecosystem as a way of keeping secrets out of the code. They&amp;rsquo;re also useful as a way of keeping the code the same between environments, e.g. live, uat and test servers but functionality might work differently.&lt;/p></description></item><item><title>Run Tests in All Packages</title><link>https://gophercoding.com/run-tests-all-packages/</link><pubDate>Fri, 03 Jun 2022 09:00:00 +0000</pubDate><guid>https://gophercoding.com/run-tests-all-packages/</guid><description>&lt;p>Life can be simple when all your Go files are in the root folder. But when a project gets a bit bigger, you may want to store much of the logic in packages. The issue comes when our usual command of &lt;code>go test&lt;/code> then no longer picks up our tests.&lt;/p></description></item><item><title>While True Loop in Go</title><link>https://gophercoding.com/while-true-loop/</link><pubDate>Fri, 03 Jun 2022 09:00:00 +0000</pubDate><guid>https://gophercoding.com/while-true-loop/</guid><description>&lt;!-- raw HTML omitted -->
&lt;p>While loops do not actually exist within &lt;strong>Go&lt;/strong> (golang), by name, but the same functionality is achievable with a &lt;code>for&lt;/code> loop. Which is great, why have a &amp;lsquo;&lt;em>while&lt;/em>&amp;rsquo; when the same can be done with &amp;lsquo;&lt;em>for&lt;/em>&amp;rsquo;.&lt;/p>
&lt;p>Our example below shows how you get a Go program to print out a message every second to the screen. (You&amp;rsquo;ll have to exit to application to stop it, e.g. Ctrl+C)&lt;/p></description></item><item><title>Hello world! Welcome to GopherCoding.com</title><link>https://gophercoding.com/hello-world/</link><pubDate>Thu, 02 Jun 2022 09:00:00 +0000</pubDate><guid>https://gophercoding.com/hello-world/</guid><description>&lt;p>Hello! This is our first post on &lt;a href="https://gophercoding.com/">GopherCoding.com&lt;/a> where we&amp;rsquo;re going to list snippets of Go (as in Golang) code to make learning and building things easier.&lt;/p>
&lt;h2 id="a-bit-of-background">A Bit of Background&lt;/h2>
&lt;p>&lt;a href="https://golang.org/">Go&lt;/a> is a programming language, heavily backed by Google, and racing up the list of most &lt;a href="https://www.tiobe.com/tiobe-index/">popular languages&lt;/a> to code in. It&amp;rsquo;s a statically typed, compiled language and is well known for it&amp;rsquo;s concurrency. Great for web applications, command lines tools and many other things.&lt;/p></description></item></channel></rss>