<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Function on Gopher Coding</title><link>https://gophercoding.com/tags/function/</link><description>Recent content in Function on Gopher Coding</description><generator>Hugo</generator><language>en-gb</language><lastBuildDate>Mon, 04 Nov 2024 09:41:37 +0000</lastBuildDate><atom:link href="https://gophercoding.com/tags/function/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>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>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>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></channel></rss>