Category Archives: Go

Golang: Sane dependency management with Glide

In a previous article I wrote an article explaining how to do dependency management wrong by following Go’s recommendations. This week I’m going to explore a better way to manage your dependencies.

Last year the Go community decided to try to fix the dependency management problem they had. Since this problem came from the root, the solution had to come from the same place. The big problem came from the fact that dependencies were pulled from GOPATH. This gave go users no way to have two versions of the same library or application installed in the same computer.

To fix this the vendor folder was created. This allows projects to store dependencies in a folder named vendor inside the project folder. This can be done recursively, so dependencies can store their own dependencies and so on. This allows each project to have it’s own dependencies without affecting other projects.

This resembles same dependency management systems, like npm. The problem is that the community didn’t provide any tooling to help you manage the dependencies. It is your responsibility to download the dependencies and put them in the vendor folder. Luckily other projects were born to help make this easier.

Read more »

Golang: Dependency management done wrong

I have just begun my journey in the Go universe and so far I have found a few things that I don’t really like. I consider this natural because as I get familiar with a way of working I find it hard to accept other ways without questioning them very heavily first.

I’m not an expert in doing dependency management, but when a friend told me how Go decided to do it, it really hurt my soul. Before I begin telling you why it did and why I believe it is the wrong way to do dependency management let me add a disclaimer:

The Go team realized that the out of the box way of doing dependency management was not ideal so they came up with a solution. If you are going to start a project that has dependencies in other projects you should use Golang’s new proposal for package management.

Read more »

Go Language: Concurrency

According to wikipedia concurrency can be defined like this:

The property of program, algorithm, or problem decomposability into order-independent or partially-ordered components or units.[1] This means that even if the concurrent units of the program, algorithm, or problem are executed out-of-order or in partial order, the result will remain determinate.

or this:

A form of computing in which several computations are executing during overlapping time periods—concurrently—instead of sequentially (one completing before the next starts)

An example of a concurrent program could be a web server that can receive multiple requests “at the same time”. If the server wasn’t concurrent, users would have to wait for another request to be processed before the server could handle their request.

Go is said to make concurrency very easy to program so today I’m going to explore it.

To explain concurrency it’s useful to have parts of a program that can execute asynchronously or in parallel. As an example I’m going to use a pair of functions that pretend to get some information from a database or service. Lets look at a simple example where everything is executed without concurrency:

Read more »

Go Language: The For loop

One of the first things I heard about Go that sounded interesting is that it has only one way to create loops. Go only provides the for statement. This sounds a little weird, but the truth is that they just decided to use the for keyword for while loops instead of having a separate while keyword. Lets see an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import (
  "fmt"
)

func main() {
  people := []string{"Hugo", "Paco", "Luis"}

  // Like a for
  numPeople := len(people)
  for i := 0; i < numPeople; i++ {
    fmt.Println(people[i])
  }

  // Like a while
  numPeople = len(people)
  i := 0
  for i < numPeople {
    fmt.Println(people[i])
    i++
  }
}

Read more »

Go Language: Slices

A few weeks ago I talked about arrays in Go. This time I’m going to cover slices, which are built on top of arrays.

In my previous post I showed the difference between doing this:

1
2
3
4
5
6
7
8
9
10
11
12
func doSomething(arr [3]int) {
    arr[0] = 5
}

func main() {
    a := [3]int{1, 2, 3}
   
    doSomething(a)

    // Prints [1 2 3]
    fmt.Println(a)
}

Read more »

Go Language: Arrays

Arrays are an interesting case in Go, because you are usually encouraged to not use them and use slices instead. Arrays in Go have a few rules that make them feel counter intuitive but I’m going to start with the parts that look normal. You can declare an array and assign values to it like this:

1
2
3
4
var a [3]int
a[0] = 5
a[1] = 11
a[2] = 22

Read more »

Go Language: Methods and Interfaces

A few weeks ago I started to learn Go and I wrote an introductory post. I’m going to continue where I left and explain how you can extend structures with methods and later how to use interfaces as arguments.

Methods

Go doesn’t have classes or objects as we know them. It uses structs instead to create object-like structures:

1
2
3
4
type Animal struct {
  color string
  size float64
}

This looks very similar to an object but something very important is missing. You can declare properties like this, but not methods. How will our animal do stuff without methods?. Golang actually does have methods, but you have to attach them to the struct after it is created:

Read more »

First steps in Go Language

My company started using Go for some services and currently there is only one person that is familiar with the language. This means nobody is reviewing the code and nobody can contribute or fix stuff if it is necessary. To fix this, I have decided to learn Go.

Install

The install steps might change depending on your operating system, so you are probably better reading the official documentation. I’m going to show the steps I followed to install on my system just as an example.

I’m running a Fedora machine so I downloaded the binary from the downloads page and extracted it to a folder. The next step is to add the Go binaries to the path. You can achieve this by adding two lines to ~/.bashrc:

Read more »