Posts with tag: golang
One of the things developers like about Go is that it has strong typing, which means that the compiler takes care of making sure that if you expect a string or a number, you will get one. Unfortunately, this approach has limitations, for example, it would be difficult to parse JSON structures since they come from the "outside world". This blog post shows how you can use one specific package from the standard library so that you can handle situations like those.
When building a library or an application in Go, we should care about having all our dependencies defined or included in the repository. If we depend on some third party code to work in a certain way, we should have some specific configuration saying that those are the versions that allow our core do its part correctly. With dep, Go's official vendoring tool it is a bit tricky since all the dependencies are moved to one place and then used across all the code. This raises a problem if we have one library in two different versions imported in two places at the same time. Fortunately, we can do something about that.
Even though one of the famous Go proverbs says _a little of copying is better than a little of dependency_, sooner or later you will find yourself importing one into your project. You just cannot solve all the problems your application may be facing, and often it's better to defer to ready, battle-tested solutions. But how should you manage your dependencies? Since we've recently (finally!) saw the premiere of an official tool, dep, let's take a look on that.
I've been meaning to play with BoltDB for a very long time, recently I finally had some time to do so. Why? I often made a mistake of setting up a large database when starting some private project only to see most of my effort going there instead of the app itself. For starters, the simplest solutions are the best, and Bolt is an example of such. Let's check out how can it be used in a small project.
When I first entered the world of Go, I had trouble thinking what are the best use-cases for the language. I knew that I could build backends for web applications or microservices, but I've always thought that it's perfect for building CLI tools. Some time ago I came across a library that makes building those in Go super easy. Let's build such a tool using Steve Francia's cobra.
Writing technical documentation for the application is often more difficult, than creating its code. We, developers, know our craft very well, but when it comes to leaving some signs of how things work, how to use them, etc., we are surprisingly ineffective. On the other hand, when we are about to use some external dependency, we'd love to see examples of how it can be inserted into our code. How do we do that in Go?
One of the things that I missed the most, apart from generics, when coming to Go while having Java background was the lack of inheritance. In JVM it was pretty simple, as you can define a parent and child classes, then have a common behavior present all the children objects. This is not the case in Go, and I had to learn how to work around this. Here, we don't have inheritance, we have the composition, and for that, we have a pretty useful technique called embedding structs.
One of the things that Go community is the proudest of is the toolchain that is shipped alongside the language. Apart from dependency vendoring (this is in progress), you can run the whole show of taking your source code, testing it, cross-compiling, all the way to building a single binary output file. One of the lesser known tools that you should get familiar with is the one that "just" lists stuff...
Recently at work, we ran into an interesting problem (challenge?). We are running our microservice architecture on Kubernetes, and we keep all of our internal communication over gRPC. Even though we are able to scale up particular parts of our applications, we started to face some bottlenecks. After a while, we realized, that even though we have multiple instances of some parts, we have one active gRPC (HTTP/2) connections between each service, which results in a single connection between particular pods.
In the previous part of the tutorial, we've generated resource endpoints for managing skills data to be displayed on a resume page. However, the problem was that anyone who could see the page, might potentially access those endpoints and edit the data. In this post, we'll add a not-so-complicated authentication using an external provider (Github) to restrict access to those pages.
We've already created a dynamic business-card website, so what can be added now? Actually, we've connected it to the database but haven't used the feature, that makes managing data really easy and fun. If you'd like to have a neat interface to view and edit stuff from the database, then you'll love Buffalo resources.
The difference between websites and web applications is that the app does display dynamic data, while a simple site can just present some static values. So far we've developed a site because nothing can ever change there without us affecting a source code. It's time to change that and finally display something from the database.
Since we started working on our business-card website using Buffalo, we might have extended our business to serve clients from all over the world. That's great, but how can we do that if our website is in English only? We definitely need to allow users to use different languages and provide appropriate content for that.
Building a web page from scratch can feel like something complicated and time-consuming. It sure can, unless we are able to split our work into multiple smaller steps by extending the functionality with every iteration. Let's take a trip with Go Buffalo framework and build a business-card website with baby steps.
Most of the applications we build need some kind of configuration that can alter its behavior. The level of logging, HTTP to run the server on, database credentials - you name it. While you can ship your app with YAML file, thanks to Docker (among others) it's more popular to use environmental variables. How exactly do we do it, though? Let's see what are our options.
Building a framework in Go is not an easy thing to do. It has little to do with the language itself, rather it's about the community approach to frameworks in general. We are about people who spend half of their time writing "if error is not nil" checks. Despite all that, a man by the name of Mark Bates chose this path and decided to create something that might revolutionize web development in Go. Let's dive into GoBuffalo, one of the most hyped Go-related things.
Have you ever noticed your web application working a bit slower than you expected? Or maybe it looks OK, but some client complains that it's slow for them? If you don't monitor your app, it's virtually impossible to verify if this is true. That's why you should use Prometheus to find out.
If you've been around the IT industry for more than a few moments, you probably already know that "it works on my machine" is no longer a valid excuse for your product misbehavior. In order to achieve the highest level of quality, we as a community of professionals have embraced an idea of Continuous Integration, and for all of you that think it's too much effort to set it up, I introduce CircleCI.
We've already seen how you can implement a simple queue in Redis using a list key and smart pushing and popping the elements. But is there any better, cleaner and more suitable way to do such a thing? Does Redis provide any other possible approach? You bet! Enter Pub/Sub.
As you probably already know, Redis goes well beyond getting and setting values. One of the less often used features in the database is the ability to implement a pretty simple task queue and share it between a producer and a consumer. Let's take a look how you can do that in a few steps.
Having implemented counting in Redis using one counter per value seems the way to go, as long as we know about all the things we want to keep track of. Once we want to monitor some more dynamic structures, this quickly becomes an issue. There is, however, a way to do this smart. We need to know what are hashes.
Since its initial release in 2009, Redis has become one of the most popular NoSQL solutions and almost a synonym for a key-value database. Thanks to being around for some time, there are a few use cases that it is more than suitable for. One of them is a persistent store for users' sessions in a web application. While it has already been implemented and available using various open source libraries, let's see if we can do it by ourselves.
Building a gRPC service is not that complicated, even if we want to add some security, custom interceptors and complex request or response messages. Unfortunately, it's still not as popular as we'd like, and most of the time we need to support HTTP as well. Building two identical APIs seems like a terrible idea, right? With gRPC Gateway you can save lots of time, so let's jump in.
Setting up communication between the server and its clients via gRPC is really simple, but what about the security? It's not that hard, either. Let's take a look.
In the previous post, we started looking on gRPC by generating code using protocol buffers. As important as that step was, it would not be quite that powerful without some actual use case, where we'd like to have the same structure created on both sides od communication. This blog post provides you with such example use case - let's see how you can build your first gRPC server and its client in Golang.
When building a microservice architecture, two of the most common approaches are a common messaging queue and HTTP APIs that are exposed and consumed by each element of the project. Despite those approaches being perceived as well-known and battle-tested, the community warmly welcomed another player to the game - gRPC. This is an introduction to this way of communication, and the story has, to begin with, a date with Protocol Buffers - something that sets gRPC apart.
So far we've seen how to build a GraphQL API that serves the user with data stored in the databases. But what about making changes? With a few tweaks in the structure, we can achieve this as well. Let's take a look on things called mutations.
Building an API in GraphQL looks very easy, as you could have already seen based on two previous posts. The thing is that everything looks simple when no parameters are provided, right? Let's take a look at how to make our API just a bit more dynamic.
In the previous post, we've seen how easy it is to set up a simple GraphQL server in Golang. The problem is, that we have hardly done anything special, that would distinguish our API from one written for REST. It's time for something more advanced, that is nested objects and fetching them from different sources.
For a long time building an API (eg. for web applications) has become associated with REST. Meanwhile, less than two years ago, Facebook announced a technology they've been using for some time called GraphQL. It's an alternative way to provide data to the users, where each client can squeeze multiple queries into a single one.
Any web application that is targeted to more than one user is likely required to have some kind of authorization technique so that it can determine if given user can execute certain actions. The problem how to do this remains, despite dozens of years passed in software development world. One of the approaches, gaining much popularity recently are JSON Web Tokens. This short post shows how you can use them in your Golang application.
In software development, everything seems to be easy until something goes terribly wrong. It might be a major crash of the database server, unexpected interaction with 3rd party service, or our main application getting really slow. How can we monitor and measure the last issue? Take a look at the ways to profile Go application.
When it's good, it's good. But what if something goes wrong in your Go application? For on apparent reason it just doesn't behave the way you wanted. You could add some console messages here and there, but is there really the only possible way to go? You absolutely need to know how to debug your code, because one day you will really need it.
Getting started with Kubernetes might seem to be slightly more complicated than with some other awesome tools you've heard about. It's because to see it in action you need to have an access to some hosting provider that allows you to deploy your containerized applications and expose them to the world. Fortunately, there is a way to play around with K8s locally - using Minikube.
If you want to deploy your application to the cloud, you may be considering to put it into a Docker container, so that you make sure the environment will be identical both in the cloud and locally. Great thing is, dockerizing your Go application won't take more that five minutes!
Writing tests first, before implementation is easy when our functions don't rely on any external tools or systems. What about unit testing the code that is responsible for interactions with the database? This requires some additional effort, but it pays off big time later on.
With Go gaining more and more popularity, it is becoming more common to create a web application with it. This, however, can be a bit problematic when it comes to writing unit tests. Fortunately, in Golang it's easier than you may think.
Handling errors in Golang seems to be a pretty easy thing to do. As a convention, whenever a function can result in some incorrect way, some error is returned as one of its values, which then can be passed further or displayed to the user. But how exactly should you do it? And is the standard package enough for all this? According to Dave Cheney, it's not.
Long gone are times when all the communication between web application server and its clients was limited to request-response communication. Nowadays, especially in systems where it is important to notify users about various events, it's absolutely necessary to use websockets in order to push information from the central point. Let's take a look on how to create simple WS server using Golang.
One of the most common rules of Golang development is that you should write code yourself as long as you can while choosing an external dependency as a last resort. This approach is completely different that that of Node JS, which is most popular for creating HTTP servers and applications. How does it look for creating web apps with Golang? Let's take a look at Iris framework, which can really speed up your development.
It is very difficult, not to say impossible, to build an application, a library of a tool without using any external dependencies. Even in Golang, where it is always preferred to do something yourself than use an 3rd party library, you need to use eg. database drivers and there is just no sense to reinvent the wheel. How should you manage your dependencies, so that any changes made by their authors don't break your work?
Isn't Go just a perfect language for microservices? Peter Bourgon certainly thought so when he started a project that was supposed to make it popular for this stuff as well. After a few months it grew into a pretty cool and interesting tool for Go developers. Let's see what you need to do to use it and how it can make your work easier.
Every now and the you face some kind of a problem in your application and come up with two (or more) solutions. You then wonder which is more efficient, which way should you go? If your problem happens to occur in a Golang application, Benchmarks come to rescue! Take a look on how you can measure performance of your code.
In the previous post we saw how to make queries to MySQL database using a small Go application. But your app will rarely be limited to read-only mode, so we obviously need to know how to save stuff to the database as well. Don't worry, it's even easier than selects!
Nowadays every application has to deal with data, which means your application has to be able to persist and read some information. You can do this using various data sources, such as files, SQL databases or NoSQL. This post deals with a thing I needed to do recently, that is fetching data from MySQL database using Go language.