Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building RESTful Web services with Go

You're reading from   Building RESTful Web services with Go Learn how to build powerful RESTful APIs with Golang that scale gracefully

Arrow left icon
Product type Paperback
Published in Dec 2017
Publisher Packt
ISBN-13 9781788294287
Length 316 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Naren Yellavula Naren Yellavula
Author Profile Icon Naren Yellavula
Naren Yellavula
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with REST API Development FREE CHAPTER 2. Handling Routing for Our REST Services 3. Working with Middleware and RPC 4. Simplifying RESTful Services with Popular Go Frameworks 5. Working with MongoDB and Go to Create REST APIs 6. Working with Protocol Buffers and GRPC 7. Working with PostgreSQL, JSON, and Go 8. Building a REST API Client in Go and Unit Testing 9. Scaling Our REST API Using Microservices 10. Deploying Our REST services 11. Using an API Gateway to Monitor and Metricize REST API 12. Handling Authentication for Our REST Services

Setting up the project and running the development server

This is a building series book. It assumes you already know the basics of Go. If not, no worries. You can jump start and learn them quickly from Go's official site at https://golang.org/. Go uses a different way of developing projects. Writing a standalone, simple program doesn't bother you much. But after learning the basics, people try to advance a step further. For that reason, as a Go developer, you should know how Go projects are laid out and the best practices to keep your code clean.

Make sure you have done the following things before proceeding:

  • Install Go compiler on your machine
  • Set GOROOT and GOPATH environment variables

There are many online references from which you can get to know the preceding details. Depending on your machine type (Windows, Linux, or macOS X), set up a working Go compiler. We see more details about GOPATH in the following section.

Demystifying GOPATH 

GOPATH is nothing but the current appointed workspace on your machine. It is an environment variable that tells the Go compiler about where your source code, binaries, and packages are placed.

The programmers coming from a Python background may know the Virtualenv tool to create multiple projects (with different Python interpreter versions) at the same time. But at a given time, one activates the environment and develops his project. Similarly,  you can have any number of Go projects on your machine. While developing, set the GOPATH to one of your projects. The Go compiler now activates that project.

It is a common practice to create a project under the home directory and set the GOPATH environment variable like this:

>mkdir /home/naren/myproject
export GOPATH=/home/naren/myproject

Now we install external packages like this:

go get -u -v github.com/gorilla/mux

 Go copies the project called mux into the currently activated project myproject.

For Go get, use the -u flag to install updated dependencies of the external package and -v to see the verbose details of installation.

A typical Go project has the following structure, as mentioned on the official Go website:

                      

Let us understand this structure before digging further:

  • bin: Stores the binary of our project; a shippable binary which can be run directly
  • pkg: Contains the package objects; a compiled program which supplies package methods
  • src: The place for your project source code, tests, and user packages

In Go, all the packages which you import into your main program have an identical structure, github.com/user/project. But who creates all these directories? Should the developer do that? Nope. It is the developer's responsibility to create directories for his/her project. It means he/she only creates the directory src/github.com/user/hello.

When a developer runs the following command, the directories bin and package are created if they did not exist before. .bin consists of the binary of our project source code and .pkg consists of all internal and external packages we use in our Go programs:

 go install github.com/user/project
You have been reading a chapter from
Building RESTful Web services with Go
Published in: Dec 2017
Publisher: Packt
ISBN-13: 9781788294287
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime