Skip to main content

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

Course Review: The Bitfountain iOS 8 with Swift Immersive

This is second big iOS learning project. For first one read iOS Programming book review. As I only have two of them done I have to compare them somehow.

I am mindful that it took me a really long time to finish it (since last autumn). For a couple of months this year I didn't even have the internet to learn it, so it could not be helped. But last half of the June I've spent watching the videos every day and managed to finish it before any new internet problems.

So how was it?
In my opinion, it's great if you're starting out. There are a lot of basics, and seeing how to do things in XCode just makes your learning much easier than looking at the photos in the book. On the other hand, learning is more shallow and it is much easier to just follow the videos and learn only the basics without bigger picture about iOS SDK. I somehow missed more challenging exercises (even though I don't like to do them ;)

Length:
Course it pretty long but many videos are quite short (1-2…

How to Learn when Life is Short

I haven't really think this through but I have read really interesting articles recently and I just wanted to write for myself later.

Life is Short by Paul Graham

It's just that, learn from people older that you what is most important, and that life actually is short...

Instead of Goals or Resolutions, Try Creating Rules by Leo Babuta

In the time of New Year Resolutions, it's good to read something that is proposing different way of achieving things. Still I see much more value in habits than setting goals. But...

The Easy Way To Learn Hard Stuff by Per Harald Borgen
It's something that I read from time to time and I know it works sometimes for me. The things I learn then are really learned and something Jakub Wasilewski  has told me many times to do. Still it's uncomfortable and for some reason, I have failed to use this method more than once or twice. Writing it here so maybe I can think about it more and figure out how to do that more often.

Towards an understandin…