Go-Lang First Steps
Easy tutorial: https://golang.org/doc/install
Official How to: #
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:
But you can't do: #
In Go, last folder name and package are the same things so you cannot even write:
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. #
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" )
import . "github.com/tj/go-debug"
In this case you have to remember to checkout project to the directory without any hyphens.
$ git clone https://github.com/krzychukula/go-newmath.git newmath
and for consistency use the same name as the package name:
3. Use "go" as a prefix without the hyphen. #
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
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: #
- 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.
- Dive into the language: https://tour.golang.org/
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
Errormethod will send the program into an infinite loop. You can avoid this by converting
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.
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/
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.
Really nice concept that when you understand can be useful in many other languages as well.
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.
Feedback or Comments?
Want to learn more?
Sign up to get a digest of my articles and interesting links via email every month.