Alexander Nnakwue Software engineer. React, Node.js, Python, and other developer tools and libraries.

Getting started with Go for frontend developers

8 min read 2461

Getting Started with Go for Frontend Developers

Introduction

Web developers coming from a purely JavaScript background would naturally pick Node.js as their backend language of choice. As a JavaScript-based runtime environment, it represents the easiest path for frontend developers to code backend functionality. Nowadays, though, there are many options for frontend developers looking to go full-stack.

Due to its paradigms and philosophy, the Go programming language might not come to mind as a top choice. As such, frontend devs might shy away from picking it up, especially those who are just getting into full-stack development.

In this article, we’ll explore these paradigms and features and why it’s worth giving Go a shot for backend/full-stack web development. We’ll also explore some new patterns, Go’s developer experience, and what to expect as a frontend dev picking up Go for the first time.

Use cases for Go

All programming languages were created to address a set of use cases or solve particular problems. Go shines in areas where efficiency and performance are top priority because it can quickly be compiled down to machine code. Thus, it doesn’t require an interpreter or a virtual machine.

Thanks to its compilation features, Go code can easily be deployed on a server infrastructure and updated across various installations accordingly. It also supports multiple OS and processor architectures, which is indeed a huge benefit.

Furthermore, Go has become a preferred language for open-source infrastructure software, with some of the largest and most popular projects being written entirely in Go. Docker, for instance, is written entirely in Go.

Another one of the great things about Go is that it gives the programmer considerable control over memory layout and allocation — much more than is typical in other garbage-collected languages.

Go implements garbage collection, concurrency, stack management, and so on via its runtime system. Automatic garbage collection makes concurrent code far easier to write, and Go has solved this developer pain point at the language design level with its ability to properly manage allocated memory.

Indeed, language design directly influences ease of use and how quickly we can build out a POC for our projects. Go shines in this regard with a set of unique features meant for highly resilient and scalable applications.

Next, we’ll explore concurrency, one very important and much talked-about feature in Go — especially for devs who are used to the synchronous nature of JavaScript.

We made a custom demo for .
No really. Click here to check it out.

Concurrency: A key feature of Go

Concurrency enables a program to organize its execution into several separate flows and communicate actions or activities between them. Concurrent programming techniques are a good way to use the CPU efficiently and improve app performance. It is an essential feature for enterprise-scale apps or apps that need to handle thousands of requests simultaneously.

Concurrency is built into the core of the Go language itself via goroutines and channels. Basically, goroutines allow applications to perform several tasks simultaneously. They are cheap, lightweight threads of execution. This affords the language better performance in terms of throughput, latency, and reliability.

If you’ve tried implementing concurrency in other programming languages, especially JavaScript, you understand that it is quite complex to do so. In contrast, spawning a goroutine is as simple as adding the go keyword before a function. Let’s see an example below.

import "fmt"

package main

func doSomething(str sting) {
     for i := 1; i <= 3; i++ {
          fmt.Printf("%s: %d", str, i)
     }
}

func main() {
    // calling this function the normal way
    doSomething("Hello")

    // Running it inside a go routine
    go doSomething("World")

    go func() {
        fmt.Print("Go routines are awesome")
    }()
}
//add return value for func

As we mentioned earlier, Go concurrency primitives via goroutines and channels make concurrent programming easy. The ability to take advantage of its multicore processor architecture and efficient memory are among the biggest reasons why Go code is running some of today’s most heavily used applications.

In fact, according to its website, Go was built for networked systems for advanced performance and full use of multicore machines:

“This means that every Go program comes with already built-in tools that can make the programs we write perform better, if properly utilized by developers.”

Note that, somewhat paradoxically, there might be only one thread in a program but thousands of goroutines. If any goroutine in that thread blocks, say, waiting for user input, then another OS thread is created, and the remaining goroutines are moved to the new OS thread. All these are taken care of by the runtime, and we, as programmers, are abstracted from these intricate details, as we have a clean API to work with.

Of course, performing actions in one goroutine means it will need to be able to communicate with other goroutines. To accomplish this, Go provides channels.

Channels are conduits that allow two concurrent goroutines to talk to one another by sending and receiving values. The channel <- syntax can be used to send a value into a channel, while the <-channel syntax can be used to read something from a channel, like so:

package main
import "fmt"
func main() {
// make a new channel
messages := make(chan string)
go func() {
// write to channel
message <- "Hello World!"
}()
// read from channel
message :=  <-messages
fmt.Printf("I got the message %s", message)
}

N.B., when reading from a channel, we block it until we get a message. The sender and receiver both have to be ready; that is, we can’t send until there is a corresponding receiver for that channel.

Why frontend devs should learn Go

For newcomers to Go, creating basic applications is a breeze, and it is also equally simple to get started and build out something quickly. Go was designed to be this way: it has a very high compilation speed, out-of-the box support for features such as concurrency and garbage collection, and, of course, Go has a very simple and clear syntax.

If you’re used to the object-oriented style of programming, you’ll find familiar concepts in Go. It has types and methods in the form of structs, which can allow an object-oriented style of programming. There are no type hierarchies; methods in Go are more general because they can be defined for any sort of data, even built-in types.

Structs have methods and properties, similar to how classes in typical object-oriented programming languages. For example:

package worker

import "fmt"

type Person struct {
    Name string
    Age int
}

func (p *Person) Talk(phrase string) {
    return fmt.Sprintf("%s says: %s", p.Name, phrase)
}

Here, Person is a struct type with fields Name and A``ge. Talk is a method defined on the Person struct that prints something. We can use this as:

person := &Person{"Vivan", 22}
person.Talk("Go is awesome")

Let’s detail some other reasons why Go has shown rapid growth in popularity in recent years, and why frontend developers looking to go full-stack should choose Go.

Easy package management

Go programs are constructed from packages, whose properties allow efficient management of dependencies. The Go toolchain has a built-in system for managing versioned sets of related packages, known as modules. Modules were introduced in Go 1.11 and have been ready for production use since 1.14.

To create a project using modules, run go mod init. This command creates a go.mod file that tracks dependency versions. To add, upgrade, or downgrade a dependency, run go get — for example, go get golang.org/x/[email protected]. See the official Go site for for more information on creating a module and additional guides on managing dependencies with modules.

Fewer language constructs and keywords

Fewer constructs and keywords means it’s easy to pick up Go and be productive in a short amount of time. Go is similar to C in terms of speed and syntax. It has around 25 keywords, making it easy to write, and overall, it’s a clear and simple language that allows developers to write readable and maintainable code.

The language does restrict the way you can do some things, though this compromise in flexibility produces greater simplicity. It might mean writing a bit more code, but there are clear solutions. Go forces consistency, which contributes to its readability and maintainability.

Go is opinionated and recommends an idiomatic way of achieving things. These come directly from the Go team based on the language design; Effective Go showcases some of these. It favors composition over inheritance, and its type system is elegant and allows for behaviors to be added or features extended to suit a particular use case without tightly coupling the components.

Unlike other languages, Go makes readability and maintainability its priority. The makers of Go added only those features to the language that are relevant and do not make the language complex.

Newer additions to the standard library are rare, for example, and the bar for inclusion is high. The creators have argued that code included in the standard library bears a large ongoing maintenance cost. The Go standard library is subject to the Go 1 compatibility promise (blocking fixes to any flaws in the API) and to the Go release schedule.

This prevents bug fixes in the standard library from being available to users quickly. As a result, new code should live outside of the standard library and be accessible via the go get command. Such code can have its own maintainers, release cycle, and compatibility guarantees.

N.B., users can find out about packages and read their documentation at godoc.org.

As another example to demonstrate the well thought-out features of the language, when a function name in a Go package starts with an uppercase letter, it means it can be exported, while all functions with lowercase names cannot. And last, but certainly not least, Go now supports generics.

Strongly typed by default

Go is a statically typed language. The compiler works hard to ensure that the code doesn’t just compile correctly, but that type conversions and compatibility are taken care of as well. This helps to prevent the issues you face in dynamically typed languages like JavaScript, wherein you discover the issues only when the code is executed.

Static code analysis

Go relies heavily on static code analysis. Examples include godoc for documentation, gofmt for code formatting, golint for code style linting, and many others. Static code analysis gives the developer a feeling of safety and peace of mind in doing things.

Inbuilt code testing utilities

Testing is a must for any program, so you should add test functions alongside actual functions each time you write code. Go provides a simple mechanism to write unit tests in parallel with code, which makes writing tests quite easy.

The tooling also provides support to understand code coverage by your tests, benchmarking tests and writing example code that is used in generating code documentation.

To create a test file, create a new file ending in _test.go in the same directory as your package sources. Inside that file, import testing and write functions of the form. We can run go test in that same directory. The script finds the test functions, builds a test binary, and runs it.

    func TestFoo(t *testing.T) {

    }

N.B., Go provides a way to test the packages that you write. With just the go test command, you are able to test code written in any *_test.go files.

Performance

As you start building production-quality applications, Go scales up nicely. Whether it’s a small microservice or an extensive enterprise application, Go offers superior performance.

Go features that contribute to its overall level of performance include concurrency, compiler optimizations, non-blocking I/O, efficient storage mechanisms for values, its runtime system, and others. Go has no virtual machine and compiles to machine code, so programs are executed fast.

As we mentioned earlier, Go has a built-in garbage collector that monitors and identifies occupied memory that is no longer needed and frees it for reuse. This lowers the risk of security vulnerabilities due to code encapsulation and also provides an effective memory management.

Built-in HTML templating engine

Go comes with a built-in HTML templating engine, which can be found in the html/template package. The syntax isn’t intuitive at first, especially coming from a frontend background, but it’s actually quite simple and gets the job done perfectly.

Large teams can easily support Go apps

Go code has a clear, neat syntax and requires little effort to learn, so developers you already work with can learn all they need within just a few of days. This will also help developers to easily support existing apps.

Moreover, Go has quite a number of online courses available and lots of tools to work with, including automatic documentation, static code analysis, embedded testing environment, race condition detection, and so on.

Another nice feature of Go is that it refuses to compile programs with unused variables or imports, trading short-term convenience for long-term build speed and program clarity. The presence of an unused variable may indicate a bug, while unused imports just slow down compilation, an effect that can become substantial as a program accumulates code (and more developers) over time.

As you can see, there are a whole lot of advantages that Go brings to both developers and business stakeholders. This is one of the reasons why the language has recently surged in popularity. Go isn’t a perfect language yet, however. You should consider the drawbacks of Go before you decide to use it for your project.

Final thoughts

The Go Project is not just the language specification, but also a toolchain and an ecosystem that consists of standard libraries and external third-party libraries contributed by organizations and individuals. The core reason behind creating the language was to help manage large code bases and improve developer productivity.

Go has seen significant adoption from large projects, and thanks to its tooling, ecosystem, and language design, programmers are steadily moving towards it, especially to build out large and highly scalable infrastructure. Getting started with Go is straightforward for any experience developer, and the Go homepage has instructions on everything from installing the toolchain to learning about Go in general.

Go is also a simple but opinionated language. Its automatic type inference is quite similar to that of dynamic languages, so it offers a familiar feel for JavaScript devs. Also, Go’s automatic documentation allows us to automatically have code documented at the same level of quality as the standard library and inbuilt APIs.

Lastly, with the strong standard library, its is awesome at building APIs/backends. Go is also cross-platform — it runs on Linux, macOS, and Windows — and comes inbuilt with useful tools for code linting, code generation, building, testing, and more.

For more details on why frontend developers moving to full-stack should choose Go, check the Go documentation FAQ page. Thank you!

: Full visibility into your web apps

LogRocket is a frontend application monitoring solution that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single-page apps.

.
Alexander Nnakwue Software engineer. React, Node.js, Python, and other developer tools and libraries.

One Reply to “Getting started with Go for frontend developers”

  1. I’m not sure but it made sense that there’s a typo in:

    // write to channel
    message <- "Hello World!"

    Shouldn't message be messages? As the name of the channel?

    Nice article though, thanks for sharing!

Leave a Reply