DEV Community

Kenny Grant for Project Page

Posted on • Edited on

Let's build something and learn go at the same time

Hi, I'm Kenny, and I write Go code for a living and run golangnews.com, a news website for Go programmers, as a hobby. I've been using Go for a while and thought I'd write some tutorials to show other people how awesome it is.

Follow along to learn go and build something awesome while you do it.

This course is an introduction to the Go programming language from Google. I'll try not to assume too much knowledge - even if you haven't done much programming before, it should be approachable with a little effort, so please ask questions in the comments below if you're unsure on any points, or think things haven't been explained well. I'm going to try a post every few days over the next few months, and by the end you should be proficient in Go and have a server up for yourself, so follow along if you'd like to learn the language in bite-size chunks.

Why would you want to learn Go? Go is ideal for writing internet servers - it powers many internet services we take for granted; it's used by companies as varied as Youtube, Monzo, Ironio, SendGrid, GitLab and Digital Ocean.

To keep us motivated, we're going to build something real - let's build a better twitter while we learn go (ignoring scaling concerns for now, we'll just keep it simple - one program, one server). I'm going to put it here: 53words.com. We'll get that server up and running in just a few lessons.

There is nothing at this domain at present, at the end of the course we'll have a better twitter (with fewer adverts, nazis and dank memes), it may also involve the number 53 somehow. So let's go!

First, we need to learn Go. It has just 25 keywords, which are a good place to start. Here are our first three keywords:

  • package - this is the basic unit of code in go, you can think of it as synonymous with folder - a folder on your computer is a package in go
  • import - this lets you import packages/folders of code from elsewhere, including the standard library of code provided by the Go authors.
  • func - this stands for function, a set of instructions for transforming data

So packages/folders contain files which contain functions which transform data.

You don't have to install anything yet, to get started, we'll use play.golang.org - this is an interactive go website that lets you execute go programs from your browser. If you open that link, you'll find this code:

// Package main is the entry point of your program
// the first and only function in your program called by the operating system
package main

// import lets you import lots of packages
import (
    // fmt is a package for reading and writing stuff (mostly text)
    // you can use it to print things out (as below)
    "fmt"
)

// func then a name defines a function
// main is the first and only function called from outside
// after that, you're on your own
func main() {

    // fmt.Println writes the things you give it to standard output
    fmt.Println("Hello", "World")

}

The quote marks above define strings (human words), we'll go into types and the different built in types in Go in the next tutorial. Let's try making a change here - add your name in quotes and print it out instead of "World", then press Run at the top to see the result below.

Back yet? Great. If you printed your name, let's try something else, add the number 53 and the text "words" at the end of the Println function and see what you get, so something like:

fmt.Println("Hello", "World", "Kenny", 53, "Words")

You'll find that Println prints most things you throw at it (even numbers or other types of thing), because it does some tricks to format every thing in the right way.

Any questions? Let me know in the comments below. You can run the final code for this lesson at play.golang.org without leaving your browser.

Top comments (0)