Skip to main content

Golang

Improving Code Quality with Testify in Go: A Deep Dive into Testing

·13 mins
Introduction # Testify is a popular testing toolkit for the Go programming language. It provides a wide range of assertion functions, test suite support, and mocking capabilities, making it a powerful tool for testing Go applications. Testify aims to simplify the process of writing tests and improve the quality of test coverage in Go applications. Testing is a critical aspect of software development that helps ensure that the software behaves as intended and catches bugs early in the development process. Writing tests can be time-consuming, but it is essential for delivering a reliable and maintainable software system. Testify can help make testing in Go more efficient and effective.

Best Logging Library for Golang

·3 mins
Introduction # I recently conducted a poll in the Go community regarding which logging library they use in their personal or professional project. The results were interesting. In this post I have jotted down the most favorite logging libraries and here is my opinion about them. Poll Results # Here are the poll results which I conducted on r/golang, The Go Programming Language community on Twitter and my LinkedIn following.

Release Dangling Elastic IPs using Lambda and Go SDK

·12 mins
Introduction # Today we are going to do a hands-on on how to kill a dangling EIP or in other words, deallocate an unassigned Elastic IP. But before that, let us know… What is an Elastic IP and why do we need it? # Elastic IPs are simply IP addresses provided by Amazon AWS. These are usually attached to internet gateways such as load balancers, or even EC2 instances. I have been using it with EC2 instances directly for my testing use case.

How to Integrate Swagger UI in Go Backend - Gin Edition

Introduction # Unlike FastAPI, Gin does not have OpenAPI integration built in. With FastAPI when you add a route, documentation is already generated. With Gin, this is not the case. But recently I integrated Swagger UI in one of my Go backends and I wanted to document that process. Prerequisites # An already existing Gin server You might consider Building a Book Store API in Golang With Gin if you are starting from scratch. I’ll be using the same book store and extending over it to integrate Swagger UI.

Building a Book Store API in Golang With Gin

·9 mins
Introduction # Go community believes that we need no framework to develop web services. And I agree with that. When you work without using any framework, you learn the ins and outs of development. But once you learn how things work, you must reside in a community and don’t reinvent the wheel. I have created POCs in Go before and I had to deal with HTTP headers, serializing/deserializing, error handling, database connections, and whatnot. But now I’ve decided to join the Gin community as it is one of the widely accepted in the software development community.

Benchmarking in Go, with Example

Introduction # From The Zen of Go: If you think it’s slow, first prove it with a benchmark Don’t assume if things are slow. Benchmark it and see if they are really slow. One thing to note here is benchmarking a program is different from profiling a program. Benchmarking is the way we check how fast our algorithm is for a given unit of the program. In benchmarking, we typically see how many iterations can a piece of code can run in a given time.

Optional Parameters Using Option Struct

·5 mins
Introduction # In last post we learned how we can use functional options to pass parameter to our functions. In this post we’ll see an alternative way to do the same thing. I’ll take the same code from the last post to keep things simple. Variadic Functions for Options # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 type Coffee struct { CoffeeBeans bool Sugar bool SteamedMilk bool } func New(opts ...func(Coffee) Coffee) Coffee { coffee := Coffee{CoffeeBeans: true} for _, opt := range opts { coffee = opt(coffee) } return coffee } // WithSugar adds sugar to the coffee. func WithSugar(coffee Coffee) Coffee { coffee.Sugar = true return coffee } // WithSteamedMilk adds steamed milk to the coffee. func WithSteamedMilk(coffee Coffee) Coffee { coffee.SteamedMilk = true return coffee } // Prepare makes a cup of coffee with given items. // Ignore the implementation for now. func (c Coffee) Prepare() string { v := reflect.ValueOf(c) typeOfC := v.Type() var items []string for i := 0; i < v.NumField(); i++ { if v.Field(i).Interface() == true { items = append(items, typeOfC.Field(i).Name) } } return fmt.Sprintf("Preparing coffee with %q:", items) } func main() { s := New(WithSugar) fmt.Println(s.Prepare()) } Here on line 1-5 we define type Coffee (or class if you would like to say). This is the analogy our code is build upon because I like coffee whenever I can.

Optional Parameters using Functional Options

·5 mins
Introduction # In Python you can assign default value to the functions, like so: 1 2 def my_func(posarg, named_arg=True, another_named_arg="Okay") # logic goes here... But in Go you can’t do this: 1 2 3 func myFunc(posarg, named_arg bool = true, another_named_arg string = "Okay") { // logic goes here... } This will throw a syntax error. Try that? In this post, we’ll see how can we overcome this issue the go way, with something called Function Options. Not sure if go is the only language that uses this syntax, if there is another language you are using, please let me know. Also, connect with me on LinkedIn

Sending POST Request in Go with a Body

Introduction # Send a POST request in golang is pretty daunting if you have a post body and you’re coming from a scripting language like JavaScript or Python. Here in Go, schema for JSON needs to be defined beforehand in order to marshal and unmarshal string back and forth to JSON. Simple POST # This marshaling/unmarshalling could be an oneliner if your request body is not nested, or is one level deep, or there is not even a body. Consider this example:

Introduction to WebAssembly with Go

I will start this post with a quote from webassembly.org: WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications.— webassembly.org That’s a lot of technical jargon. In very simple terms, Wasm provides a way to run code written in multiple languages on the web at near-native speed. Where multiple languages refers to all these languages.

Introduction to BoltDB

Why use BoltDB # Bolt plays pretty well with Go’s concurrency model, we can do multiple reads concurrently. For pro and cons, I will contrast it with similar database SQLite. I have done SQLite when I worked with Xentrix to develop GUI tools for the artists. You may want to connect with me on LinkedIn. Pro # It is native go. This means you don’t need any database server running, like SQLite. In oppose to Redis and memcached.

Unit Testing, Test Coverage and CI with Travis in Go

You can’t think of deploying your application to production without testing it. Neither you can manage a large codebase with confidence without it. Let us go through some basics of unit testing in golang. This post is structured in the following manner: unit testing basics in golang (jump) inbuilt code coverage command understanding subtests and helper function (jump) Travis CI integration (jump) running test against multiple version of go Please connect with me on LinkedIn and let’s get started.