Setup & project structure

Alright, let's go! We'll keep this part short, so you can start building. ๐Ÿ˜Ž

Get the files

First, make sure you have Go, git, and Docker installed. Then:

  1. Go to github.com/golangdk/canvas and press the big, green button that says "Use this template".
  2. Fill in your repository name, and whether you're coding in public or in private. Now, press the button "Create repository from template".
  3. When Github has created the repository for you, clone it to your machine. I recommend GitHub Desktop for a nice, free git GUI, which makes it easy to clone from Github as well.
  4. On your terminal in the project directory, run:

    $ git remote add golangdk https://github.com/golangdk/canvas.git && git fetch golangdk

    This will make it easier to keep up to date with changes from the original canvas repository.

  5. Open the repository in your favorite editor. If you don't have a favorite Go editor yet, I recommend GoLand by JetBrains. VS Code is also a very popular choice for Go development.

You will now have a very basic Go project structure with a few config files, a Makefile, and the initial files for your Go program.

Project structure

Everyone has different preferences for project structure, and there is no right way to do it. That said, in canvas we follow some conventions that work quite well:

  • Config files, the go.mod/go.sum files, the Makefile, and more, are in the root folder.
  • The cmd/server/ directory has the main package and function that starts the server.
  • The server/ directory will hold the HTTP server setup and running code.
  • The handlers/ directory will have all the HTTP handlers.

The Makefile is a convenience so that we have to type less. It uses the go command for testing, coverage, and starting the server.

Try running make start, and you should see a nerdy smiley on your terminal:

$ make start
go run cmd/server/*.go
๐Ÿค“

To run your tests and see coverage, run make test cover:

$ make test cover
go test -coverprofile=cover.out -short ./...
?   	canvas/cmd/server	[no test files]
?   	canvas/handlers	[no test files]
ok  	canvas/server	0.286s	coverage: [no statements] [no tests to run]
go tool cover -html=cover.out

That's what you need to know for now. Next, let's write some code!

Review questions

Sign up or log in to get review questions with teacher feedback by email! ๐Ÿ“ง

Questions?

Get help at support@golang.dk.