Book Image

Echo Quick Start Guide

By : Ben Huson
Book Image

Echo Quick Start Guide

By: Ben Huson

Overview of this book

Echo is a leading framework for creating web applications with the Go language.  This book will show you how to develop scalable real-world web apps, RESTful services, and backend systems with Echo.  After a thorough understanding of the basics, you'll be introduced to all the concepts for a building real-world web system with Echo. You will start with the the Go HTTP standard library, and setting up your work environment. You will move on to Echo handlers, group routing, data binding, and middleware processing. After that, you will learn how to test your Go application and use templates.  By the end of this book you will be able to build your very own high performance apps using Echo. A Quick Start Guide is a focussed, shorter title which provides a faster paced introduction to a technology. They are for people who don’t need all the detail at this point in their learning curve. The presentation has been streamlined to concentrate on the things you really need to know, rather than everything.
Table of Contents (10 chapters)

Setting up Echo

After you have a working installation of Go, you will be able to install the Echo web application framework. To install third-party libraries and packages, you can use the go get command to get any package you need. The following is how you would go about installing Echo version 3.3.5, which is the latest version at the time of writing this book, and which is the version of Echo we will use for the remainder of this book:

go get github.com/labstack/echo

cd $GOPATH/src/github.com/labstack/echo

git checkout tags/3.3.5

cd -

The preceding commands will clone the Echo web application framework into your GOPATH in the appropriate location, and then checkout the 3.3.5 release tag of the repository. With the preceding command, we can ensure that we are using the correct version of Echo. Cloning the repository directly and checking out the appropriate version might seem a little bit annoying, but unfortunately the go get tool, at the time of writing, does not support versioned package downloads. In future chapter examples, we will utilize a version pinning tool that is extremely handy called dep, which will eliminate the need to perform these steps on a per project basis.

In order to verify that we have your environment completely set up, we will now create a small Echo-based web application, and compile it to prove we have a working setup. You can download the following code using git with the following commands:

mkdir -p $GOPATH/src/github.com/PacktPublishing/

cd $GOPATH/src/github.com/PacktPublishing/

git clone https://github.com/PacktPublishing/Echo-Essentials

cd Echo-Essentials

Now that you have the code, you will be able to see the following code located in the ./chapter1/environment_setup.go: file:

package main
import (
"net/http"
"github.com/labstack/echo"
)

func main() {
// create a new echo instance
e := echo.New()
// Route / to handler function
e.GET("/", handler)
// start the server, and log if it fails
e.Logger.Fatal(e.Start(":8080"))
}

// handler - Simple handler to make sure environment is setup
func handler(c echo.Context) error {
// return the string "Hello World" as the response body
// with an http.StatusOK (200) status
return c.String(http.StatusOK, "Hello World")
}

Within this project, you may notice that all of the dependencies are already downloaded to the ./chapter1/vendor directory. If you wanted to pull the dependencies yourself, this can be performed by running go get ./... from within the $GOPATH/src/github.com/PacktPublishing/Echo-Essentials/chapter1/ directory. The go get command will walk through your source code recursively and pull all of the dependencies needed from the internet, based on the import statements from within your code. At this point, you should attempt to run the code with go run environment_setup.go, which should produce the following output:

   go run $GOPATH/src/PacktPublishing/Echo-Essentials/chapter1/environment_setup.go 
____ __
/ __/___/ / ___
/ _// __/ _ \/ _ \
/___/\__/_//_/\___/ v3.3.5
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
O\
⇨ http server started on [::]:8080

This output proves that we are properly configured for our Echo-based web application, and that we are using the correct version of Echo, that is Version 3.3.5.