<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Go on The Blog of Boban Acimovic</title><link>https://acim.net/categories/go/</link><description>Recent content in Go on The Blog of Boban Acimovic</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><copyright>The Blog of Boban Acimovic &amp;copy; 2026</copyright><lastBuildDate>Sun, 16 Jul 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://acim.net/categories/go/index.xml" rel="self" type="application/rss+xml"/><item><title>What's new in pgx v5: CollectRows, RowToStructByName, CollectOneRow, RowToStructByPos and ForEachRow</title><link>https://acim.net/blog/pgx-v5-3/</link><pubDate>Sun, 16 Jul 2023 00:00:00 +0000</pubDate><guid>https://acim.net/blog/pgx-v5-3/</guid><description>&lt;p&gt;This is the third part of the &lt;code&gt;pgx v5&lt;/code&gt; series. Let&amp;rsquo;s discuss the new features from the title.&lt;/p&gt;

&lt;h3 class="relative group"&gt;CollectRows and RowToStructByName
 &lt;div id="collectrows-and-rowtostructbyname" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#collectrows-and-rowtostructbyname" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;CollectRows&lt;/code&gt; allow us to fetch an array of rows and set them into a slice of structs. Further, &lt;code&gt;RowToStructByName&lt;/code&gt; is a long awaited feature which allows to store a row into a struct. &lt;a href="https://github.com/jmoiron/sqlx" target="_blank" rel="noopener"&gt;sqlx&lt;/a&gt;
 module became very popular in Go community exactly for this reason, but &lt;code&gt;pgx&lt;/code&gt; now supports something similar. &lt;code&gt;RowToStructByName&lt;/code&gt; is a generic function of the &lt;code&gt;func(row CollectableRow) (T, error)&lt;/code&gt; and it&amp;rsquo;s not the only one of this type, there are also &lt;code&gt;RowTo&lt;/code&gt;, &lt;code&gt;RowToStructByPos&lt;/code&gt; and &lt;code&gt;RowToStructByNameLax&lt;/code&gt;. Under the hood, &lt;code&gt;RowToStructByName&lt;/code&gt; is using reflection so it may be slightly slower than classic way of scanning the rows, but if you don&amp;rsquo;t mind this, it is very easy to use. All field names are going to be mapped to lowercased public struct field names and you may also use &lt;code&gt;db&lt;/code&gt; struct tags if some field name differs from the struct field name. This explanation may sound difficult, but the following example will actually show that it is actually very easy to use.&lt;/p&gt;</description></item><item><title>What's new in pgx v5: QueryRewriter and NamedArgs</title><link>https://acim.net/blog/pgx-v5-2/</link><pubDate>Sat, 15 Jul 2023 00:00:00 +0000</pubDate><guid>https://acim.net/blog/pgx-v5-2/</guid><description>&lt;p&gt;In this second part of the &lt;code&gt;pgx v5&lt;/code&gt; series, we are going to discuss the &lt;code&gt;QueryRewriter&lt;/code&gt; interface and &lt;code&gt;NamedArgs&lt;/code&gt;. Let&amp;rsquo;s first check out the signature of the &lt;code&gt;Exec&lt;/code&gt; method of the &lt;code&gt;pgxpool.Pool&lt;/code&gt; struct.&lt;/p&gt;</description></item><item><title>What's new in pgx v5: Introduction</title><link>https://acim.net/blog/pgx-v5-1/</link><pubDate>Sun, 04 Dec 2022 00:00:00 +0000</pubDate><guid>https://acim.net/blog/pgx-v5-1/</guid><description>&lt;p&gt;Go has very rich standard library which also contains &lt;a href="https://pkg.go.dev/database/sql" target="_blank" rel="noopener"&gt;database/sql&lt;/a&gt;
 module with generic interface over SQL databases. Further, &lt;a href="https://github.com/lib/pq" target="_blank" rel="noopener"&gt;lib/pq&lt;/a&gt;
 &lt;code&gt;PostgreSQL&lt;/code&gt; driver is fully compatible with &lt;code&gt;database/sql&lt;/code&gt; and provides all basic database operations. However, since this is generic implementation over all supported databases, using it is a trade-off not supporting some PostgreSQL specific features. Fans of &lt;code&gt;PostgreSQL&lt;/code&gt; (in further text &lt;code&gt;pg&lt;/code&gt;) may want more and here comes &lt;a href="https://github.com/jackc/pgx" target="_blank" rel="noopener"&gt;pgx&lt;/a&gt;
 in help. &lt;code&gt;pgx&lt;/code&gt; is a very rich &lt;code&gt;pg&lt;/code&gt; driver supporting &lt;code&gt;LISTEN / NOTIFY&lt;/code&gt;, &lt;code&gt;COPY&lt;/code&gt;, type mapping between &lt;code&gt;pg&lt;/code&gt; and &lt;code&gt;Go&lt;/code&gt;, all specific &lt;code&gt;pg&lt;/code&gt; types, wire protocol and much more.&lt;/p&gt;</description></item><item><title>Check type of generic parameter in Go</title><link>https://acim.net/blog/check-type-of-generic-parameter/</link><pubDate>Sat, 03 Dec 2022 00:00:00 +0000</pubDate><guid>https://acim.net/blog/check-type-of-generic-parameter/</guid><description>&lt;p&gt;Go is over ten years old language but generics (type parameters) are introduced just in the March this year. Hence, still lot of developers avoid using them or have certain problems once decided to give it a try. One of the common problems is how to check the concrete type of a generic parameter, specially of non constrained parameters. So, if you try to do the following, it won&amp;rsquo;t work:&lt;/p&gt;</description></item><item><title>How to write complex HTTP middlewares in Go</title><link>https://acim.net/blog/complex-go-middlewares/</link><pubDate>Sun, 13 Nov 2022 00:00:00 +0000</pubDate><guid>https://acim.net/blog/complex-go-middlewares/</guid><description>&lt;p&gt;Lot of the tutorials on the Web show different ways of writing Go HTTP middlewares, but most of them use functional approach meaning using functions that get dependencies as parameters and return HTTP handler function or handler. There is nothing wrong about this, but it can be quite messy to write a complex middlewares like database based authentication and authorization or simply middlewares which have lot of dependencies. In this article I am going to explain how to use more object oriented way of writing such middlewares. Let&amp;rsquo;s start with the basic object oriented middleware example:&lt;/p&gt;</description></item><item><title>Custom Go HTTP handlers using generics</title><link>https://acim.net/blog/custom-generic-go-http-handlers/</link><pubDate>Sun, 19 Dec 2021 00:00:00 +0000</pubDate><guid>https://acim.net/blog/custom-generic-go-http-handlers/</guid><description>&lt;p&gt;Few days ago Go 1.18beta1 was released and with it the first official generics support. I was embarrassed with standard Go&amp;rsquo;s HTTP handler functions for quite a while. If you are not familiar with Go, you probably wonder why, but if you are familiar, I believe you know. For example, implementing a RESTful API using idiomatic Go requires lot of code repetition in order to JSON decode request bodies and JSON encode response bodies. This problem was possible to solve in some way but never in such elegant way like using generics.&lt;/p&gt;</description></item><item><title>VSCode setup for Go developers</title><link>https://acim.net/blog/vscode-go/</link><pubDate>Tue, 16 Mar 2021 17:00:00 +0000</pubDate><guid>https://acim.net/blog/vscode-go/</guid><description>&lt;p&gt;According to lot of online polls, Visual Studio Code is the most popular IDE among Go developers. You can start Go development immediately after installing &lt;a href="https://marketplace.visualstudio.com/items?itemName=golang.Go" target="_blank" rel="noopener"&gt;Go extension&lt;/a&gt;
 for VSCode, but some tuning may make your life easier. First of all, you may want to format the source code and reorganize imports automatically on each file save or code paste. In order to do so, put the following JSON snippet in your &lt;code&gt;settings.json&lt;/code&gt;:&lt;/p&gt;</description></item><item><title>Upcoming Go's generics</title><link>https://acim.net/blog/upcoming-gos-generics/</link><pubDate>Fri, 26 Feb 2021 00:00:00 +0000</pubDate><guid>https://acim.net/blog/upcoming-gos-generics/</guid><description>&lt;p&gt;In the very early adoption of Go by the open source community, lot of developers already suggested the lack of generics. However, some other claimed that simplicity of Go shouldn&amp;rsquo;t be compromised by generics. Few days ago, the never ending discussions pro and contra generics are finally over. The proposal is accepted and we can expect generics in Go 1.18 somewhere in spring of 2022. You may wonder why does it take that long, but this is not just about the compiler but also some parts of the standard library may need to be rewritten in order to replace the old code which could benefit from generics.&lt;/p&gt;</description></item><item><title>Go vanity imports</title><link>https://acim.net/blog/go-vanity-imports/</link><pubDate>Sun, 21 Feb 2021 00:00:00 +0000</pubDate><guid>https://acim.net/blog/go-vanity-imports/</guid><description>&lt;p&gt;If you have just started learning Go or already developing in Go for quite some time, you may noticed that most of the Go packages are imported directly from their respective VCS repositories, but there are some packages imported from URL&amp;rsquo;s like &lt;a href="https://golang.org/x/text" target="_blank" rel="noopener"&gt;golang.org/x/text&lt;/a&gt;
 or &lt;a href="https://go.uber.org/zap" target="_blank" rel="noopener"&gt;go.uber.org/zap&lt;/a&gt;
. If you try to visit such URL&amp;rsquo;s, you may see different results like being redirected to documentation or just some dummy page. This is completely different from packages hosted on GitHub, for example &lt;a href="https://github.com/pkg/errors" target="_blank" rel="noopener"&gt;github.com/pkg/errors&lt;/a&gt;
 where you can see the real source code. This feature of Go is called vanity imports.&lt;/p&gt;</description></item><item><title>A first impression of Rust from the perspective of a Go developer</title><link>https://acim.net/blog/rust-from-the-perspective-of-a-go-developer/</link><pubDate>Sat, 26 Dec 2020 00:00:00 +0000</pubDate><guid>https://acim.net/blog/rust-from-the-perspective-of-a-go-developer/</guid><description>&lt;p&gt;In these uncertain times and multiple months of remote work, at least we have some more time to dive into something new. I have been developing in Go since 2017, learned some TypeScript in the meantime, but few days ago I wanted to get into Rust once again. First time, approximately a year ago I gave up very quickly, realizing that Rust requires lot of time to learn and I didn&amp;rsquo;t have that much time at the moment.&lt;/p&gt;</description></item><item><title>Changes in Go implementation of the new protobuf and gRPC</title><link>https://acim.net/blog/changes-in-go-implementation-of-the-new-protobuf-and-grpc/</link><pubDate>Sun, 15 Nov 2020 00:00:00 +0000</pubDate><guid>https://acim.net/blog/changes-in-go-implementation-of-the-new-protobuf-and-grpc/</guid><description>&lt;p&gt;If you don&amp;rsquo;t know yet, there were &lt;a href="https://github.com/protocolbuffers/protobuf-go" target="_blank" rel="noopener"&gt;major changes by Google&lt;/a&gt;
 in protobuf and gRPC implementation for Go. New version should be better in all means so you should switch as soon as possible, if you are already using these technologies. In this article I am going to focus on Linux variant, but the upgrade procedure should be quite similar on OSX or Windows.&lt;/p&gt;</description></item><item><title>Serving Single-Page Applications in Go</title><link>https://acim.net/blog/serving-spa/</link><pubDate>Sun, 03 May 2020 00:00:00 +0000</pubDate><guid>https://acim.net/blog/serving-spa/</guid><description>&lt;p&gt;Someone may ask what is so special about serving single-page applications? Well, if you use hash (#) based URL&amp;rsquo;s like &lt;a href="https://yourdomain.com/#/yourroute" target="_blank" rel="noopener"&gt;&lt;em&gt;https://yourdomain.com/#/yourroute&lt;/em&gt;&lt;/a&gt;
, there is no problem because browser recognize that this is a default page with some parameters and doesn&amp;rsquo;t make another request to the server. However, if you want to use non-hash based routes, there is a problem if you try to refresh a non-default route. In this case browser&amp;rsquo;s default action is to load such URL from the server and there is no such document on the server. For example, if you load your SPA at URL &lt;a href="https://yourcomain.com" target="_blank" rel="noopener"&gt;&lt;em&gt;https://yourdomain.com&lt;/em&gt;&lt;/a&gt;
 and then you click on another route, you will see in your browser&amp;rsquo;s URL field something like &lt;a href="https://yourdomain.com/yourroute" target="_blank" rel="noopener"&gt;&lt;em&gt;https://yourdomain.com/yourroute&lt;/em&gt;&lt;/a&gt;
. That works fine unless you try to reload the page. In that case browser doesn&amp;rsquo;t know this is actually already loaded SPA, but it actually tries to fetch &lt;em&gt;/yourroute&lt;/em&gt; URI from server. As there is no such URI on the server, you will usually get the famous 404 error and your application breaks.&lt;/p&gt;</description></item><item><title>Generators, wait groups and closures in Golang</title><link>https://acim.net/blog/generators-waitgroups-closures-golang/</link><pubDate>Fri, 04 Oct 2019 00:00:00 +0000</pubDate><guid>https://acim.net/blog/generators-waitgroups-closures-golang/</guid><description>&lt;p&gt;This is the first step of my intention to write a series of articles where I would explain some common patterns used in Golang. Let&amp;rsquo;s start with &lt;a href="https://en.wikipedia.org/wiki/Generator_%28computer_programming%29" target="_blank" rel="noopener"&gt;generator pattern&lt;/a&gt;
:&lt;/p&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span style="display:flex;"&gt;&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"&gt;1&lt;/span&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;type&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Generator&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;func&lt;/span&gt;() &lt;span style="color:#f92672"&gt;&amp;lt;-&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;chan&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Generator is a function or method which returns a sequence of values. In Golang, this usually means returning a channel of values of desired type. In our example, we will return a channel of integers. Let&amp;rsquo;s see an implementation of this:&lt;/p&gt;</description></item><item><title>Install Golang package on Ubuntu Linux</title><link>https://acim.net/blog/install-golang-ppa/</link><pubDate>Thu, 03 Oct 2019 00:00:00 +0000</pubDate><guid>https://acim.net/blog/install-golang-ppa/</guid><description>&lt;p&gt;Official Golang documentation contains just tar.gz package to be &lt;a href="https://golang.org/dl/" target="_blank" rel="noopener"&gt;downloaded&lt;/a&gt;
 and installed on Ubuntu Linux, but this is not quite comfortable way because Golang won&amp;rsquo;t be automatically updated after new release. Fortunatelly, there is &lt;a href="https://github.com/golang/go/wiki/Ubuntu" target="_blank" rel="noopener"&gt;trustable PPA&lt;/a&gt;
 that can be used.&lt;/p&gt;</description></item></channel></rss>