Re: Isolation

It’s easy to talk about SOLID principles, design patterns, testing, moral expectations, self responsibility, and any other guide lines for a better world. Sometimes it’s even easy to adhere to those ideas, to put them in practice, no matter how may times some people repeat “there wasn’t/isn’t time”.

Although, there are situations that really stand against all best practices, like big legacy products which are already on the market, used by many clients, developed by many people who are managed very strict, in order to maintain… order. Or you need to get fast on the market.

The minimum that can be done is isolation. The least you can do is to throw your code in its own corner and use it only from there, then go back to change it whenever you want, without fear, with minimum cost and risk.

Handling API errors

The past days I’ve practiced Go by writing a package for Apixu weather service. They have a a straightforward REST service to offer their weather information.

Their service also returns an error response if it can’t offer data based on your needs, if you use an invalid API key, or for other cases. Of course, the Go package should also return errors if the case.

No problem with returning data, but I had some issues handling errors. There can be general errors that have nothing to do with Apixu (but with the package internals) and errors returned by them. My first approach was to return three values for each API method:

Search(q string) (Search, ApixuError, error)

But it smelled right away. There had to be another way. Continue reading Handling API errors

Reloading Go apps automatically while developing

There are some ways to automatically reload Go apps while developing, to not have to manually stop your app, build it, run it. Recently I ran into this article about a nice tool called Fresh.

I wanted to start using live reload (or hot reload) for a project of mine, but it had a particularity which gave me troubles when I tried Fresh. My case was:

  • I had a multiple apps Git repository
  • The apps were sharing some packages
  • If I edited app1 and the packages it used, I didn’t want both app1 and app2 to be restarted, only app1 (similar for app2)

Continue reading Reloading Go apps automatically while developing

My learning process

In the 7th grade I got my first PC. I’ve immediately embraced everything I could, I was curious about all the things. I’m talking about advanced, scientific things like changing the wallpaper, running the defragmenter, formating a partition, using music applications, installing games and software for friends, knowing what RAM, ROM, HDD and others are.

And in the 9th grade I had my first encounter with programming. It was Turbo Pascal. Then, I don’t remember why and how, I ran into the web world. First, it was HTML and CSS, which I wrote in Notepad, of course. Then started with PHP and MySQL, and probably in the same period I met JavaScript.

Along the years I peeked at Visual Fox Pro, C, C#, Python, Visual Basic, Java, and maybe other languages, too. Small stuff, nothing important. I was just curious.

I guess web programming was easier to get started with and that’s why I insisted on it. And I liked it. A lot. Continue reading My learning process

Counting number of items in a concurrent map

Lately I’ve been using Go’s concurrent map. And sometimes I needed to count the items I’ve stored in the map. I have ranged over the items and incremented a counter, and it was done:

package main

import (
       "sync"
       "log"
)

func main() {
       m := sync.Map{}
       
       m.Store("k1", "v1")
       m.Store("k2", "v2")
       
       count := 0
       m.Range(func(key, value interface{}) bool{
              count++
              return true
       })

       log.Println(count)
}

But I didn’t want to range over the items each time I wanted to count them. So I thought of incrementing the counter every time an element was added, and to decrementing it when one was removed. The following is just for practice, I didn’t perform any advanced tests. Continue reading Counting number of items in a concurrent map

Go HTTP server duplicate handler call

It took me a minute or two to figure out why, while doing some experiments, for the following built-in HTTP server use case, the handler was called two times, and so printing the log message two times.

package main

import (
       "log"
       "net/http"
)

func main() {
       http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
              log.Println("call")
       })

       http.ListenAndServe(":8800", nil)
}

The path “/” catches all requests, thus it catches the “/favicon.ico” request sent by the browser. So I’ve just used another path.

Two sides of working with junior developers

Seeing people evolve around you gives you a fulfilling. Even more if you were there for them giving advice, sharing ideas and links, lending a book, explaining principles, answering to HOWs and WHYs, making up a step-by-step plan to help them better organize themselves.

If they’re unemployed, maybe you help them get a job. If they’re coworkers, you push them to ask more from themselves regarding the big picture of the project, how to search for help, how to detach sometimes from their mindset and try something new.

And very important, you make them ask themselves how others solved different situations and why they applied some principles. You help them identify needs and track down bugs.

Or…  Continue reading Two sides of working with junior developers

Go web app with all assets embedded into the binary

From my first interactions with Go, a very small web app came out. There were several microservices that were exposing data through REST and I needed to quickly browse all of the content in a user friendly way.

I wanted something fast, simple, and easy to integrate new microservices on in the future, as it was the first Go app in the company. I quickly wrote it, deployed it, and the job was done.

To make the deploy process very easy, I wanted a one file application, so I used the go-bindata package to embed the HTML files into the binary, and the CSS and JS files (jQuery and Bootstrap) were served from the official CDNs (now, to show a full example, I’ve embedded all of them).

Take a look at the Micro UI source code.

From PHP to Go

Besides being very powerful, Go is a clean language. It was easy to get started with it, despite it has some obvious major differences if coming from a language like PHP. I knew some of them, cause they’re specific to any compiled language, while in an interpreted one you have to work in order to get their benefits.

I got comfortable with them and even wished PHP had them. I’m not comparing the two languages by considering one to be better or worse, I’m only telling some differences that caught my eye, even if they are normal to be.

It got me happy about writing code in a very different way, putting aside some things that are normal in other contexts and I was used to. Continue reading From PHP to Go

Go concurrency is elegant and simple

These days I wanted to speed up some data retrieval with Go. Its concurrency model is elegant and simple, it has everything you need built-in.

Let’s say there are some articles that need to be fetched from an API. I have the IDSs of all the articles, and I can fetch them one by one. One request can take even a second, so I added a 1 second sleep to simulate this.

type Article struct {
       ID    uint
       Title string
}

func GetArticle(ID uint) Article {
       time.Sleep(time.Second * 1)
       return Article{ID, fmt.Sprintf("Title %d", ID)}
}

The classic way of doing this is making a request for each article, wait for it to finish, store the data.

var articles []Article
var id uint

for id = 1; id <= 10; id++ {
       log.Println(fmt.Sprintf("Fetching article %d...", id))
       article := GetArticle(id)
       articles = append(articles, article)
}

log.Println(articles)

With a 1 second response time it takes 10 seconds. Now imagine 100 articles or more. Continue reading Go concurrency is elegant and simple