/StudyGroup

Go London Study Group

Primary LanguageHTMLGNU Affero General Public License v3.0AGPL-3.0

London Gophers Study Group

func main() {
    fmt.Println("Hello, London Gophers!")
}

Code of Conduct

We follow the Go Code of Conduct adapted from the Contributor Covenant, version 1.4; in summary:

  • Treat everyone with respect and kindness.
  • Be thoughtful in how you communicate.
  • Don’t be destructive or inflammatory.

From GopherCon 2018: Natalie Pistunovich - The Importance of Beginners:

"No question is stupid. When you're [...] answering beginners' questions, do it well, be nice, be informative, and mainly be patient. If you see a question for the hundredth time, you don't have to answer. Answers like 'just use the standard library' are not helpful and they're not friendly. Somebody else can do this who is maybe a bit more fresh."

The Workshop

Visit the London Gophers Study Group Meetup for details of our IRL study group meetings.

Find us on Slack in the #london-study-group channel of the gophers.slack.com workspace. To join this Slack workspace, start here.

Our study material is: The Go Programming Language by Alan Donovan and Brian W. Kernighan

Work through the material at a speed that suits you. We also have open space meetups to meet to chat about Go and the book etc. So just join in any time! :)

1. Begin

Just tell me how to write Go code

There's a great how to write Go code document here -> golang.org/doc/code.html :)

Ok, the workshop...

To get access to the repo ask an organiser at one of the meetups to add you. Then to begin, create your own directory in the workspaces directory, work through the book exercises, and add your code there. When committing to the repo just commit directly to master but before you do so make sure you run git pull -r (-r to rebase) before you push, to avoid merge commits.

It may be easier to instead run:

git config branch.autosetuprebase always

...in the repository directory to ensure that git pull always performs a rebase, rather than a merge, on git pull.

To do this for all repositories (a global setting) use:

git config --global branch.autosetuprebase always

You can add your code in separate directories if you're using Go Modules or if you're using a GOPATH you could structure your code as in the GOPATH Project Structure section.

If you need the exercises from the book, they're available in exercises.tar.gz zipped archive but these are already in the book so you might not need them.

2. Installing Go

a. Home/Linuxbrew

It may be convenient to install the latest version of Go through the Homebrew and Linuxbrew package managers.

brew install go

b. Install with Binary Distributions

The https://golang.org/dl/ page contains distros for Windows, MacOS, Linux, and source.

The installation instructions explain how to install them.

3. Getting the Workshop Code

To obtain all of the source code examples used in the book (many of which are code examples to be amended) you can get this by running in your workspace:

git clone https://github.com/adonovan/gopl.io.git

(This will get the helloworld code, plus all the other examples).

Remember to add a .gitignore file containing:

gopl.io

...so you don't check in that example code to the repo.

4. Building and Running Go

There are two main options, by using Go Modules or by setting a GOPATH. You may wish to use a GOPATH pointing to all of your workspace code but you might find modules more appropriate and this is now the recommended approach.

a. Running Go with Go Modules

Go modules are available in Go 1.11 onwards which removes the need to have a $GOPATH set.

To use modules in your project directory, run:

go mod init MODULE

...where MODULE is the name of your module. For these exercises this could be something simple such as studygroup but could be a domain name and path such as github.com/somebody/component. This will create a file called go.mod.

To include an external dependency to your project just add the import, such as:

package main

import "github.com/some/dependency"

func main() {
    dependency.f()
}

When running your code this module should be automatically downloaded and added to your go.mod file which could look like:

module github.com/somebody/component

require (
    github.com/some/dependency v1.2.3
)

Because we have a number of separate components in one place we probably want to run things individually rather than all samples together. Something like this will run one exercise:

go run ./$MODULE/ch1/ex1_1

...assuming your code is in the path ./$MODULE/ch1/ex1_1 underneath your go.mod file.

b. Running Go with a GOPATH

The traditional way to run/build Go code (prior to Go Modules) is using a GOPATH. You should set your $GOPATH to your current directory, such as:

export GOPATH=/home/gopherg/eng-golang-workshop/workspaces/gogopher

To run some code you can then use:

go run gopl.io/ch1/helloworld

(which actually builds then executes /home/gopherg/eng-golang-workshop/workspaces/gogopher/src/gopl.io/ch1/helloworld/main.go)

To build it and output in your $GOPATH\bin directory:

go build -o $GOPATH/bin/helloworld gopl.io/ch1/helloworld

To get another module (such as the imaginary some/dependency):

go get github.com/some/dependency

...and this will then be downloaded to $GOPATH/src/github.com/some/dependency and imported with import "github.com/some/dependency"

GOPATH Project Structure

When using GOPATH your project structure may be something like:

workspaces
    gogopher\
        bin\
            helloworld
            ...
        src\
            gogopher.io\
                ch1\
                    ex1_1\
                        main.go
                ...

5. Development Environments

a. Delve (Debugger)

Delve is is a debugger for Go. To install, run:

go get -u github.com/go-delve/delve/cmd/dlv

To see the available commands, run dlv then help at the (dlv) prompt.

b. GoLand

GoLand is a new commercial Go IDE by JetBrains aimed at providing an ergonomic environment for Go development.

The new IDE extends the IntelliJ platform with coding assistance and tool integrations specific for the Go language.

c. Emacs

If you follow similar instructions to get Go support for emacs (OS X) from here and you run into the following error when trying to get auto-complete to work:

Error running timer ‘ac-update-greedy’: (file-missing "Searching for program" "No such file or directory" "gocode")
Error running timer ‘ac-show-menu’: (file-missing "Searching for program" "No such file or directory" "gocode")
Error running timer ‘ac-update-greedy’: (file-missing "Searching for program" "No such file or directory" "gocode"

...then the problem is probably down to gocode not being available in your path.

So if you edit /etc/paths.d/go and add the path to the bin directory of your project it should fix the problem.

d. Visual Studio Code

Visual Studio Code is a lightweight but powerful source code editor with support for debugging, embedded Git control, syntax highlighting, intelligent code completion, snippets, and code refactoring. Visual Studio Code is based on Electron and uses the Blink layout engine.

VSCode uses the same editor component as Atom (codenamed "Monaco").

The Go extension for Visual Studio Code provides language features such as IntelliSense, code navigation, symbol search, bracket matching, snippets etc.

e. acme

There are three kinds of IDEs:

  • A character driven IDE such as unix, emacs or vi
  • A closed environment with its own bespoke tooling such as Eclipse, Visual Studio Code, IntelliJ, Atom, GoLand
  • An integrating environment that integrates tools from outside inwards such as plan9 and acme.

Vague Installation Guide

f. Atom

Atom supports Go development with the go-plus package.

To use Delve inside Atom, install the go-debug package.

To run your Go code in Atom, install the atom-runner package.

6. Links

A Tour of Go

How to Write Go Code

Effective Go

Source code: The Go Programming Language

YouTube: Concurrency is not Parallelism by Rob Pike

YouTube: Go Proverbs

7. Rights

All exercises from The Go Programming Language are copyright 2016 Alan A. A. Donovan & Brian W. Kernighan and included with permission from the authors.

All submitted code is covered under Apache License 2.0.