
I personally know about the
Go general purpose programming language for a long time. I think many of you have heard of him. Reading various blogs, I found descriptions of experiments of developers with this wonderful platform (and this is a platform, not only a programming language in itself). I do not know why, but I sincerely believed that Go is too young to focus its attention on it. And in vain. As it turned out, recently wonderful application libraries and frameworks have started to appear actively, which personally, as a web developer, are extremely interesting to me, since they combine high performance (after all, Go is a compiled language) and relative ease of use . I want to tell about one such web framework written on Go. So, get acquainted,
web framework Revel .
First, I will ask the community for some indulgence - I just started diving into Go, I mainly use information from foreign sources, and therefore I may be somewhat confused in Russian terminology. Nevertheless, I hope that my work will be useful to someone. This article is mainly a translation of the official framework documentation with some of my explanations.As it appears from the main page of the official site, Revel is the implementation of the familiar to many
Play web framework! on go.
')
Environment preparation
Start by
installing Go . It is assumed, of course, that you have Linux.
Set the variable GOPATH
If you did not create this variable during the Go installation, do it now. GOPATH is the path to the root directory where all your Go source codes will live. To create this variable, do the following:
- Create a directory:
mkdir ~ / gocode
- Let Go tell it to use this directory:
export GOPATH = ~ / gocode
- Save GOPATH so that this variable is subsequently used automatically:
echo GOPATH = $ GOPATH >> .bash_profile
That's it, the Go installation is complete.
Install Git and Mercurial
Both of these version control systems are used in Go to get packages using the
go get
command.
Install Revel
go get github.com/robfig/revel
During the execution of this command several things will happen:
- Go, using Git, will clone the framework repository at
$GOPATH/src/github.com/robfig/revel/
- Go will consistently resolve all dependencies and install the necessary packages using
go get
Build Revel for Commandline Use
This will help us build, run, and package our applications.
To do this, from the GOPATH directory, execute the following command:
go build -o bin/revel github.com/robfig/revel/cmd
Add the path to the Revel binaries to the
PATH
variable to use the
revel
from any directory
export PATH="$PATH:$GOPATH/bin" echo 'PATH="$PATH:$GOPATH/bin"' >> .bash_profile
And finally, check how everything works.
$ revel help ~ ~ revel! http://robfig.github.com/revel ~ usage: revel command [arguments] The commands are: run run a Revel application new create a skeleton Revel application clean clean a Revel application's temp files package package a Revel application (eg for deployment) Use "revel help [command]" for more information.
All right, now we can start creating.
Create an application
And, of course, we will use our favorite command line. First, create an empty application:
$ cd $GOPATH $ revel new src/myapp ~ ~ revel! http://robfig.github.com/revel ~ Your application is ready: src/myapp</pre>
Now run it:
$ revel run myapp ~ ~ revel! http://robfig.github.com/revel ~ 2012/09/27 17:01:54 run.go:41: Running myapp (myapp) in dev mode 2012/09/27 17:01:54 harness.go:112: Listening on :9000
Now open the browser to make sure everything works.

File structure in Revel applications
gocode app controllers models views tests conf app.conf routes public css CSS js Javascript images
Directory / app
The
app
directory, as is usually the case, contains the source codes of our application: controllers, models and templates.
app/controllers
app/models
app/views
Revel keeps track of the contents of this directory and, as soon as it sees the changes, recompiles your application. Very comfortably. But if changes occur outside this directory, Revel will not react to them. This is the task of the developer.
Directory / public
Here static files are stored, and, as it usually happens, they are divided into three subdirectories. Again, everything is pretty typical.
Directory / conf
Contains configuration files. Two main configuration files:
app.conf
- the file with the main settings of the applicationroutes
- route definitions
Routs
Roads, I remind you, are in our
conf/routes
file, which in our case will have the following content:
GET / Application.Index
As you have already guessed, this means that in the case of a GET request to the “root” of the site, the
Index
method in the
Application
controller will be called.
Controllers
Let's see the contents of this controller. The file is in
app/controllers/app.go
:
package controllers import "github.com/robfig/revel" type Application struct { *rev.Controller } func (c Application) Index() rev.Result { return c.Render() }
All controllers should be inherited (so to speak in Go terms) from
rev.Controller
or from
*rev.Controller
. All controller action methods must return
rev.Result
.
Revel controllers support many convenient methods for generating a query result (Result). In this example, the
Render()
function is used, which searches for the corresponding template, parses it and returns the result to the browser, simultaneously returning the status 200 ok.
Templates
All templates should be in the
app/views
folder. When the template is not specified explicitly, Revel takes the template corresponding to the action. For example, in our case, the
app/views/Application/Index.html
template will be used:
{{set . "title" "Home"}} {{template "header.html" .}} <h1>Your Application Is Ready</h1> {{template "footer.html" .}}
In addition to the basic functions that are used in Go templates, Revel adds a number of its own. The specified template is very simple. Is he:
- Sets page title
- Incorporate a header template (I understand that it is necessary to write in Russian, but I do not know how to write it in Russian so that it is not clumsy)
- Displays greeting
- Included footer pattern
The contents of the header.html template look like this:
<!DOCTYPE html> <html> <head> <title>{{.title}}</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" media="screen" href="/public/stylesheets/main.css"> <link rel="shortcut icon" type="image/png" href="/public/images/favicon.png"> <script src="/public/javascripts/jquery-1.5.2.min.js" type="text/javascript" charset="utf-8"></script> {{range .moreStyles}} <link rel="stylesheet" type="text/css" href="/public/{{.}}"> {{end}} {{range .moreScripts}} <script src="/public/{{.}}" type="text/javascript" charset="utf-8"></script> {{end}} </head> <body>
You can clearly see where the title is inserted, and you can still see that the template accepts additional style files and javascripts - pay attention to the
moreStyles
and
moreScripts
respectively.
Pleasant trifles
Let's change the greeting. To do this, edit the contents of the Index.html
Index.html
:
<h1>Your Application Is Ready</h1>
on:
<h1>Hello, world!</h1>
Now, if you re-open your browser and refresh our page, you will see how its content has changed. Revel tracked the template change and made it a hot reboot. It also tracks changes to:
- Content directory
app/
- Templates inside
app/views/
- Routes that are described in the
conf/routes
file
Changes to these files will be automatically taken into account in Revel. Let's see what happens if we change the contents of our controller. For example, this:
return c.Render()
change it to:
return c.Renderx()
We intentionally made a mistake. Refresh the page and you will see a notification:

And finally, let's try to pass some data to our template. Edit our controller:
return c.Renderx()
change to:
greeting := "Aloha World" return c.Render(greeting)
Now the template file. Change:
<h1>Hello, world!</h1>
on:
<h1>{{.greeting}}</h1>
Now refresh the page and see how Revel greets us in Hawaiian:

Some encouraging conclusions
I spent quite a long time searching for a platform for developing high-performance web applications. For a long time, hesitated when choosing between the “classics” (for example, Python with his Tornado), the controversial Node.js (with his connect.js and express.js) and still an exotic Erlang. The situation is exacerbated by endless holivars around these platforms, especially around everything related to Node.js and Erlang. Holivary holivary, but the share of common sense in the attacks on Node.js, however, is present. I don’t consider Python for subjective reasons (there’s no point in writing - I don’t want to hurt anyone), and Erlang is Erlang, it’s too exotic, although I have respect and awe in front of it.
I do not claim that Go as a whole and Revel in particular are a panacea or a silver bullet. But they, in my opinion, are quite similar to this silver bullet, although there are still enough shortcomings. For example, the lack of Revel ORM (although this disadvantage is easily compensated by the presence of a
large number of third-party libraries .
At the moment, Go is a very powerful tool with all the advantages of compiling languages, and even having a noble origin. I think that choosing Go as a platform for your project, you are unlikely to regret it in the future. Moreover, as you can see, there is interest in the language, libraries are emerging.
By the way, if you are concerned about the issue of launching and running Go under Windows - he can do it perfectly. Personally, I now practice it under Windows
(well, it happened) .