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

Create a navigation menu for your Android app

I finished building a hobby app a few weeks ago, but after getting all the functionality right I couldn’t help but notice that it looked horrible. I’m going to slowly try to make it less ugly starting with this post.

The first thing that I want to do is get rid of the default title bar because it occupies too much space:

title-bar-android

Create the file src/main/res/values/style.xml if it doesn’t exist already and create a new theme with no title:

Read More

Using the Gradle wrapper on your Android project

I have an android project I’ve been working on for a few weeks. I got a new computer recently and I wanted to work on this project. I downloaded the Android SDK and gradle. When I tried to run a build:

1
gradle assembleDebug

I got this error:

1
Gradle version 2.2 is required. Current version is 2.11. If using the gradle wrapper, try editing the distributionUrl in /home/you/repos/asdf/gradle/wrapper/gradle-wrapper.properties to gradle-2.2-all.zip
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

Add one to a number without using plus or minus sign

I got asked this question in a code interview and I wanted to make sure my answer was good. Without the pressure of being in an interview I see the problem more clearly and the problem seems pretty easy now.

Lets look at the basics of adding in binary:

1
2
3
4
 0         1       1
+0        +0      +1
---       ---     ---
 0         1      10
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

Debugging with tcpdump

I’m having some problems with one of my hobby servers but this time instead of looking at the code to try to figure out what is happening, I decided to try to do it using only tcpdump. I was trying to start my server and I got this error message:

1
failed to create GA auth provider: invalid character '\u003c' looking for beginning of value

The character ‘\u003c’ translates to <, so it seemed like the problem was that somewhere in my GA auth library I was getting what looked like an HTML instead of a JSON. The first thing I did was monitor the HTTP traffic using this command:

1
tcpdump -c 20 -s 0 -i eth0 -A tcp port http
Read More

Restart a process automatically if it dies

I have a hobby server that I’m deploying to a digital ocean droplet. I run this server as any other program and it does what it was programmed to do:

1
./myserver

The problem is that this server is not perfect and I’m OK with that. Nevertheless, I don’t want to have to restart it manually every time it dies. For that reason I did some googling and found an easy way to restart my server if it unexpectedly dies:

1
2
3
4
5
6
7
8
#!/usr/bin/env bash

until /home/tacos/myserver >> myserver.log 2>> myserver.error.log; do
    echo "$(date -u) - Server crashed with exit code $?.  Respawning..." >> runner.log
    sleep 1
done

echo "$(date -u) - Server manually killed" >> runner.log
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