In some of my projects we have code that needs to run external executables, and it’s very difficult to test them, especially when your function is based on some kind of stdout parcing. So how to mock these commands in Go? Let’s check how this goal is achieved in os/exec package. In exec_test.go we can see a helperCommand
. When running go tests, the go tool compiles an executable from your code, runs it and passes all the flags. Thus, while your tests are running, os.Args[0]
is the name of the test executable. So the executable is already there and runnable, by definition.
Swagger UI is a great tool and a must have for any respectable API project. It has an intuitive design, all endpoints can be tested from the interface. For example, let’s have a look at Kubernetes API, where endpoints are grouped by version, and everything is accessible in easy way. In this post I’ll show how to build it together with your API written in Go.
Today we found that our web server written in Go has memory leaks and consume around 300M of memory, which is really a lot for our app. After restart it’s back to ~10M but each hour increased by few more. Golang has nice built-in tools to debug and find leaks.
The Go language comes with a powerful built-in template engine. In Go, we have the template package to help handle templates. We can use functions like Parse
, ParseFile
and Execute
to load templates from plain text or files, then evaluate the dynamic parts. Also it’s possible to create user-defined functions and call it from templates.
Today I saw that the size of my vendor/
folder in Golang project is around 150M. I am using glide
there and there are 24 dependencies (it’s a program with multiple data storage connectors, notifications, etc.), so I decided to review it and reduce the amount of 3rd party libraries.
What does data race mean in Golang? Data race is a common mistake in concurrent systems. A data race occurs when two goroutines access the same variable concurrently and at least one of the accesses is a write. Itβs really hard to recognize it without specific tools or without an eagle eye, because when you run a program itβs always a chance that you wonβt see your mistake or it will be very transparent.
This post shows how to marshall NULL values from the database into Go struct and how to avoid mistakes during fetching optional values with SELECT query. I’ll show standard types sql.NullString, sql.NullInt64, etc types.
~21MB
Well, I found yesterday that LogPacker Daemon weights about 21MB. This application is written in Go language, it’s really doing a lot of things, has built-in connectors to different Data-Storages, has Cluster solution inside, etc.
30 basic questions to measure your Golang knowledge. Answers and .go files you can find on GitHub page.
Don’t hesitate to create a Pull Request in case of some found errors, typos.
go test -bench=.
Go has a great option to write your benchmarks and run it together with go test with option -bench. To create a benchmark function you must do the following: