Skip to content

Latest commit

 

History

History
243 lines (210 loc) · 13.5 KB

102_advanced_go.md

File metadata and controls

243 lines (210 loc) · 13.5 KB

Golang programming course

Table of Contents

Why Golang?

Go was created by Google out of need. Why don't we let the creators themselves explain exactly why. This is also a great video where Sameer Ajmani from Google explains the need for Golang

Golang in short

Golang is a fairly new language built from the ground up, developed by Google and released in 2009.
Go is somewhere between the compiled languages as C and C++, and the interpreted languages as Java. Go is a compiled language and outputs only one single binary file. But when working on a Go project you can also run the program as an interpreted language without having to compile first.
Go also has object oriented-like features but uses them in a slighty different way. Go has no classes nor inheritence but uses alternative mechanisms to define relationships.

Code

Go code is easy to write, clean and minimalistic and gofmt formats the code for you so every code looks the same. This makes working on large projects with lots of developers al lot easier.

Packages

Packages in Go and are very complete, powerful and versatile. They will save you a lot of time when, for instance, implementing network protocols, hashes and IO operations.

Concurrency

One of the great features in Golang is concurrency. In Go it's fairly easy to write a program running multiple processes at the same time. The combination of concurrency and the powerful packages is the reason why programs as Docker and Kubernetes are written in Go.

Install Golang

Go to the official website and follow the install instructions.
Ubuntu users can follow this guid Make you set the GOPATH correctly according to your OS.
To unlock the full potential of Spacemacs and Golang, follow these steps:

  1. Add these lines to your .bash_profile or .bashrc depending on OS:
export GOPATH=$HOME/go # or the path you want Go to live in.
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
  1. Make sure your GOPATH has these three folders:
  • bin
  • pkg
  • src
  1. When you use Spacemacs as your editor, you can use life-changing enhcancements as godef, go-eldoc and guru, install these packages:
go get -u -v github.com/nsf/gocode
go get -u -v github.com/rogpeppe/godef
go get -u -v golang.org/x/tools/cmd/guru
go get -u -v golang.org/x/tools/cmd/gorename
go get -u -v golang.org/x/tools/cmd/goimports

Enabling goimports, open the .spacemacs file and look for: (defun dotspacemacs/user-init ()
Then add: (setq gofmt-command "goimports")
To automatically use syntax highlighting, add this line: (setq-default go-guru-hl-identifier-mode t)
So the code looks like this:

(defun dotspacemacs/user-init ()
  (setq gofmt-command "goimports")
  (setq-default go-guru-hl-identifier-mode t)
  "Initialization function for user code.
It is called immediately after `dotspacemacs/init', before layer configuration
executes.
This function is mostly useful for variables that need to be set
before packages are loaded. If you are unsure, you should try in setting them in
`dotspacemacs/user-config' first."
)
  1. In order automatically use the highlighting in every buffer, add the (add-hook 'go-mode-hook #'go-guru-hl-identifier-mode) below (defun dotspacemacs/user-config () so it looks like this:
  (defun dotspacemacs/user-config ()
    (add-hook 'go-mode-hook #'go-guru-hl-identifier-mode)
    "Configuration function for user code.
  This function is called at the very end of Spacemacs initialization after
  layers configuration.
  This is the place where most of your configurations should be done. Unless it is
  explicitly specified that a variable should be set before a package is loaded,
  you should place your code here."
    )
  1. In order to reload Spacemacs, press SPC f e R
  2. Now open Spacemacs and create a .go file byt typing SPC f f and typing in your filename plus .go extension. Spacemacs will now import the go layer and is able to use all the installed packages.
    To get a grasp of how these functions work, you an read this document and watch this video

Update october 2018
Some guidelines to install Spacemacs on Linux.
Install Emacs:
apt install emacs (sudo when needed or other package manager for your system.)
Check if Emacs is 24.4 or above:
emacs --version
If Emacs is below 24.4:
apt install emacs25
Read the Spacemacs install instructions here.
In short:

  • Clone the repository in the right location: git clone https://github.com/syl20bnr/spacemacs ~/.emacs.d
  • Launch Emacs: emacs or when elpa needs an insecure http connection: emacs --insecure
    Spacemacs will be automaticly installed. Restart Emacs to complete the installation.
    When creating a .go file, Spacemacs will ask to install the Go-layer. Press yes.

Make sure you set your paths right otherwise these plugins below won't work!
Put this in your .profile / .bash_profile file:

export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export GOBIN=$HOME/go/bin
export PATH=$PATH:/usr/local/go/bin:/home/<user>/go/bin

Somehow Linux doesn't always like environmental vars ($GOPATH and$GOROOT) as part of the PATH declaration so use absolute paths.

To unlock some awesome tools in Go, read this website.
In short:

go get -u -v github.com/nsf/gocode
go get -u -v github.com/rogpeppe/godef
go get -u -v golang.org/x/tools/cmd/guru
go get -u -v golang.org/x/tools/cmd/gorename
go get -u -v golang.org/x/tools/cmd/goimports

If you want to use gometalinter:

go get -u -v github.com/alecthomas/gometalinter
gometalinter --install --update

Only thing left is to activate some layers.
Open the .spacemacs file in the .emacs.d folder or press SPC f e d in Emacs.
Look for dotspacemacs-configuration-layers and un-comment auto-completion and syntax-chacking.
You're probably set up now. Reboot your machine to make sure everything is loaded property.

If the autocompletion isn't working, you need to enable auto-complete-mode. Open the .spacemacs file SPC SPC, enable auto-complete-mode and restart spacemacs.

Learning go

These are some useful introductions to Go:

  • A Tour of Go by Google.
  • Safaribook Online - Introduction to Go
    For this course you need to login. Ask us for the proper login information.
  • Miek Gieben has made a nice book about Learning Go(pdf warning).
  • Caleb Doxsey wrote this introduction for Go both on the web as a PDF
  • Alan Donovan and Brian Kernighan wrote The Go Programming Language.

Additional resources

  • Information for all the packages in Go from the official website
  • Dave Cheney has a nice page with valuable resources for Go.
  • A curated list of awesome Go frameworks, libraries and software.
  • A series of walkthroughs to help you understand the Go standard library better.

Nice excercises

Test your skills and see if you can crack these excersises.
See also the various answers by other users.

Interesting Go talks

Use the power

Go has a few features that and packages that make Go so powerful and easy to use. It's worth paying extra attention to the items in the list below.

  • Interfaces
    You can use interfaces in a few different ways to really boost your code. Read this post by Steve Francia about structs, methods and interfaces and how it relates to OOP.
  • Goroutines and channels
    Concurrency in Go is powerful when knowing how to use it. This is a great video where Rob Pike explains Go channels, goroutines and patterns with example code.
    Divan has made concurrency visual for you and explains this in a video.
    Tutorial about concurrency and wait-groups
  • Packages You don't have to re-invent the wheel. Go has core packages that so you don't have to build everything from scratch. Learning how to use them will make your life a lot easier:

Testing

Go has a testing package package.
Video on testing in Go.

Reference

The complete Go Programming Language Specification
Tutorial on for loops

Must read stuff

  • A string is not a string
  • Blogpost about strings in Go.
  • Strings to bytes and why
  • Great explanation on how Go appends to slices for you and what happens internally
  • Getting a grasp on pointers

Protocols

Learn about all the different protocols you'll probably going to work with.

HTTP

Great explanation of the HTTP protocol and how it communicates can be found here and here

Websocket

WebSockets provide a persistent connection between a client and server that both parties can use to start sending data at any time.
A websocket begins as a HTTP request but then upgrades to a websocket connection.

Basics

  • Understand pipes STDIN / STDOUT / STDERR about

Go and Docker

  • Nice blogpost how to use Go and Docker.
  • Build standalone Docker images that run Go.
  • Complete repository with different Go versions and base images.

Dep

Dep is a dependency management tool for the Go language. With go get you can install packages (imports) in your GOPATH folder so that they are available for every project. However, when you are working on lots of different project, you don't always want to install all the packages. That's where Dep comes in.
Dep allows you to install and maintain the packages (and which versions) you need just for that project.
Check the dep website for how to use dep.