New blog domain: kula.blog

It's still work in progress but new posts will be published on  https://kula.blog/ Thanks to 11ty base blog RSS is working from the start so it should be easy to add to your reader if you still use any :) If now then I hope you can sign up to the newsletter where I'll be publishing new posts and interesting articles to it from time to time. Please use  kula.blog  from now on.

Go-Lang First Steps

Installation


Official How to: 


Available also as a video:



Github

I have noticed that TJ Holowaychuk is creating a lot of projects with "go-" prefix. I thought it might be a good idea as well. The thing is you can't use such packages out of the box.

Default import looks like:

import "github.com/tj/go-debug"

But you can't do:


go-debug.debug("sending mail")

In Go, last folder name and package are the same things so you cannot even write:

package go-debug


If it is so hard, why to do it? For me, most important thing is to easily see which projects are written in Go. You cannot use this project in the folder with the same name as the repository name. So how are others dealing with it?

How to use projects on Github with "go" prefix?

1. Create a directory in the repository to be used as the namespace name. 

Example: https://github.com/google/go-github

import "github.com/google/go-github/github"

This way your project can have language prefix and your package name can be a single word. On the other hand adding yet another level of directories is just annoying most of the time. Creating many packages in this manner creates much more jumps between directories.

2. Use go-* prefix in project name and import it without it. 


import ( auth "github.com/abbot/go-http-auth" )
or:

import . "github.com/tj/go-debug"

In this case you have to remember to checkout project to the directory without any hyphens.

For example:

$ git clone https://github.com/krzychukula/go-newmath.git newmath
and for consistency use the same name as the package name:

package newmath


3. Use "go" as a prefix without the hyphen. 

Example: https://github.com/krzychukula/goheroku

This is probably the easiest one. Downside: you have to use "go" everywhere. Seeing packages you use at a glance is no longer possible. Everything will be

  • goauth
  • goweb
  • gofiles

You have to decide for yourself.

4. Just name a project not technology 

This one makes a good sense to me if the project is more general. For example command line tool, or a page that can change to a different technology at some point. 


Learning the language:


  1. As a first step, I can recommend Writing Web Application. Especially if you are already familiar with writing them in other languages. For me, it was really fun and much easier than going through all the features without any context. 
  2. Dive into the language: https://tour.golang.org/

Tour

This part is not so easy. You have to write code in a new language and understand Go way of doing things at the some time. In my opinion, it is ok to skip some parts as long as you plan to get back to them when you will be more comfortable with the language. For example set yourself a reminder to do it in a couple of days.

Difficulty of steps is diverse, most of them are just about seeing new construct but from time to time you have to write code in textarea ;)

Hard part for me started at: #51: Methods and Interfaces

First puzzling thing for me was: Exercise #58: Errors

Note: a call to fmt.Print(e) inside the Error method will send the program into an infinite loop. You can avoid this by converting e first:fmt.Print(float64(e)). Why?
I had to ask for help. Fortunately, thanks to help of Kuba I have understood how Error function on new Type is by default implementing Error interface, so that Print knows this new type should be printed using Error.

Goroutines: Big picture - really similar to Generators in ES6. If you understand generators it should be easy for you to understand Gorutines.

I probably have to go through all of the examples from #50 to #73 and write all of them myself on my machine instead of web interface.

Book



I also know about book that you can read online for free. I want to read more about structs and interfaces for sure. http://www.golang-book.com/

Summary

Go is a nice language. Especially basic stuff like code formatting, "switch" or "if" statements are idiomatic and easy to understand. Multiple return and multiple assignments are something that I have to get used to, but my first impression is positive. I really like the ease of use of command line 'go' utility. I could run, test or format files without googling how to do it.

Structs, Interfaces and Pointers
Here will be dragons. Easy stuff is over, and you should watch your step. I am not saying that it is automatically bad, it is just much different and hard to grasp on one sitting.

Gorutines
Really nice concept that when you understand can be useful in many other languages as well.

Web Applications
Standard Library is rich. Easy to follow tutorial impressed me. For the start, it might be a good idea to just use Go in a real environment for a while and learn more advanced features one be one.


Comments

Popular posts from this blog

How to simulate slow connection (developer proxy in Node.js)

Safari Mobile CSS VW and VH units bug.