CODE GIST: https://gist.github.com/tyndyll/cce72c16dc112cbe7ffac44dbb1dc5e8
A high level introduction to the Go programming language, including a sample Hello World web server
caveat emptor
I’m an engineer, not a computer scientist… these are the opinions of someone that has been using Go in production for 5+ years and likes it because it gets stuff done,
not because it is some version of computing purity
What is Go? Go (often referred to
as Golang) is a
statically typed,
compiled
programming
language designed at
Google
– Wikipedia
A definition of Go as a programming language. Static typed, so we are having to declare what type are variables are, and compiled, so that to run our code we need to
use the go compiler to execute the code
Why is Go?
Asking the question Why is Go is important, as the context it was created in, and the problems it was trying to solve explains the rationalisation behind the language and
why some decisions were made
– Rob Pike
“The goals of the Go project were to eliminate the
slowness and clumsiness of software development at
Google, and thereby to make the process more productive
and scalable. The language was designed by and for
people who write—and read and debug and maintain—
large software systems.”
Google was originally created at Google in 2007. According to Rob Pike, one of Go’s authors at his talk at Splash 2012 Google was having issues regarding build speeds,
on boarding developers and drift across their code. His presentation gives a lot of context
https://talks.golang.org/2012/splash.article
His talks are always worth a watch.
The conclusion we can come to is that Go was created, not as a exercise in language design, but rather an attempt to solve software engineering problems.
It must work at scale
It must be familiar, roughly C-like.
It must be modern.
dl.google.com
These are the three conclusions that the Go development team came up with initially. Working at scale refers to both running the software, and to the number and size of
teams. Familiar and C like refers to the fact that Java, C++ and Python were the developers that Google had and were hiring at the time so it had to take that into
account. Must be modern refers to the fact that it was being developed in 2007. At this time Google had been working in the web for a considerable time, and new what
was required for success in this environment - HTTP, cryptography, concurrency and memory protection.
dl.google.com was one of the first services written in Go that was released into production. This services handles all downloads at Google - Chrome, Android, SDKs - so
it is heavily loaded and well tested
This is the code we are going to be looking at. It’s a simple Hello World application, but it is a web server, with some concurrency built in, so that when a request to say
hello comes in, it will return, but also pass the request through to a Printer service
The code and more comments and commentary, including how to execute and build the code, will be found at
https://gist.github.com/tyndyll/cce72c16dc112cbe7ffac44dbb1dc5e8
This slide shows packages and how they are imported. This `main` package is the entry point to the program. The two packages imported, `fmt` and `net/http` are part of
the standard library. The lower code snippets show how external packages are imported. These are pulled by using the built in tool `go get`, which will clone code from a
repository. This repository can be public or private. Once imported, the packages would be available as `alexa.TypeName`. For more see
https://tour.golang.org/basics/1
This is a demonstration of how structs are implemented. See the gist for further details
https://gist.github.com/tyndyll/cce72c16dc112cbe7ffac44dbb1dc5e8
What if I want to log?
This is a demonstration of how interfaces are implemented. The interface declares that this interface is satisfied by any struct that has a `Print(string)` function. There are
no additional keywords such as implements. Also note that there are no public or private keywords. This is because this is implemented in go by making functions and
types that have a capital letter Public, and functions and types with a lower case letter private.
Something else of interest - if the developer decides that simply printing the string is not enough and they want a timestamped log entry instead, they can import the log
by adding it to the import statement above, and changing the fmt on line 15 to log. However, this will not compile. One of the features of Go is that it demands that
unused code is removed. This is in response to the problem of non required dependencies existing, and causing confusion in future development. Removing the fmt
package would allow the file to compile again
See the gist for further details, including an additional implementation of a Printer
https://gist.github.com/tyndyll/cce72c16dc112cbe7ffac44dbb1dc5e8
This is where all of the action happens. As in many programming languages, main is the programs entry point. See the gist for further commentary
https://gist.github.com/tyndyll/cce72c16dc112cbe7ffac44dbb1dc5e8
This page lists a number of companies and products that are using Go heavily. This is not to indicate that writing in Go will create a billion dollar unicorn, but more to
demonstrate that many types of products (networking, APIs, tooling, databases, smart contracts) are being developed and heavily used in production across the industry
Similarly, with our Belfast focus, these are companies locally that are using Go everyday. Learning Go isn’t niche, and there are jobs and opportunities available
Tooling
• Built in Libraries
• Built in tools…
• Testing
• Deployment
Tooling is one of the primary features of Go, and it’s one of the reasons that Go is scalable across teams. Aside from the fantastic standard library (https://golang.org/
pkg/), Go has a huge number of tools as part of the distribution (https://golang.org/cmd/). The standout is gofmt, which formats code into the Right Way. This makes Go
code extremely readable no matter what team has written it. go test is also built in, which makes testing a first class component of Go.
Deployment is interesting also. Go is a compiled language, producing a single binary with all libraries statically compiled into it. Compiling for different platforms or
architectures is as simple as setting the GOOS (darwin, linux, windows) or GOARCH (386, amd64, arm etc) environmental variables respectively. There is more
information in the `Compiling` section of the gist. Also of interest - Go is now available as AWS Lambdas or Google Cloud Functions, as well as being an ideal candidate
for docker containers.
Learning and Support
•Tiny base language
•Decisions made for
you
•Good docs
•IDE’s
•Helpful community
•Playground
•Fast development
cycle
I believe that Go makes an excellent teaching language, and as such is easy to pick up (25 keywords with a fully populated standard library). While static typing may
seem more complicated than dynamic typing, the compiler is helpful in its error messages.
Go is opinionated, deciding what the style of code should be and enforcing it both through gofmt and the compiler. You may have strong opinions about where curly
braces should be placed, but Go really doesn’t care. This is enforced through the compiler, for example, by not requiring semi colons to terminate statements and only
accepting newlines or braces.
The docs are great, and are locally available in the distribution by executing godoc -http :6060 (makes them available in a browser on port 6060). They are also built into
your code in a similar way to Javadocs (https://blog.golang.org/godoc-documenting-go-code)
Recommended IDE’s
Goland - https://www.jetbrains.com/go/
VS Code - https://code.visualstudio.com/docs/languages/go
If you want to just dabble rather than setting everything up have a look at Golang Playground
http://play.golang.org
What can’t be underestimated is the fast compile times. Compiling even large projects takes seconds, to the point where running `go run code.go` makes for a really
good scripting environment
So It’s Perfect? Well…
• Dependencies are
complicated
• Generics
• Error handling
These are currently being
actively addressed by the
community
I could explain these, but it’s easier to go straight to the source. The community is getting better at discussing their issues in public and using the community to help
Dependencies issues and conversation - https://github.com/golang/go/wiki/Modules
Generics issue and conversation - https://go.googlesource.com/proposal/+/master/design/go2draft-generics-overview.md
Error handling - https://go.googlesource.com/proposal/+/master/design/go2draft-error-handling-overview.md
• Easy to pick up
• Scales to Teams
• Feature packed standard
library
• Fantastic Tooling
• Good community
• 2.0 is coming
So Why Go?
Summing everything up
• Web services
• Networking services
• Command line tools
• Glue…
• Multi platform
• Lambda/Cloud Functions
So When Go?
When would you use it? Ironically on the morning of the talk Garth Gilmour (https://twitter.com/GarthGilmour) retweeted this
https://twitter.com/GarthGilmour/status/1084760622397575168
If you want to try Go, it’s design makes this really straightforward. Download and install, and get started. Everything is possible in a single file, managing dependencies is
straightforward, if it is required (again, you can go far on the standard library alone). If you have a simple idea for anything like the above, I strongly encourage you to give
Go a look. If I can help, my Twitter handle is on the next slide
With regard to “Glue” above. Using C libraries in Go is pretty straightforward. If you have something you need a C library for, why not wrap it in Go?
https://blog.golang.org/c-go-cgo