Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

The easiest way to create web applications with Go

branch: google-app-eng…

This branch is 2 commits ahead and 0 commits behind master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 examples
Octocat-spinner-32 .gitignore
Octocat-spinner-32 LICENSE
Octocat-spinner-32 Makefile
Octocat-spinner-32 Readme.md
Octocat-spinner-32 cookie.go
Octocat-spinner-32 fcgi.go
Octocat-spinner-32 request.go
Octocat-spinner-32 scgi.go
Octocat-spinner-32 servefile.go
Octocat-spinner-32 status.go
Octocat-spinner-32 web.go
Octocat-spinner-32 web_test.go
Readme.md

web.go

web.go is the simplest way to write web applications in the Go programming language. It's ideal for writing simple, performant backend web services.

Overview

web.go should be familiar to people who've developed websites with higher-level web frameworks like sinatra or web.py. It is designed to be a lightweight web framework that doesn't impose any scaffolding on the user. Some features include:

  • Routing to url handlers based on regular expressions
  • Secure cookies
  • Support for fastcgi and scgi
  • Web applications are compiled to native code. This means very fast execution and page render speed
  • Serving static files

Installation

Make sure you have the a working Go environment. See the install instructions. web.go targets the Go weekly release. Go is a fast-changing language, and it's easier to keep with the weekly branch than to maintain separate branches.

To use web.go with Go's weekly branch:

  1. Run hg update -r weekly. If you're running an outdated version of Go, or the release version, it likely won't compile.
  2. git clone git://github.com/hoisie/web.go.git
  3. cd web.go && make install

You can also install using goinstall github.com/hoisie/web.go, but if you do this, the import statement in your go programs will be import github.com/hoisie/web.go instead of just import web.

Example

package main

import (
    "web"
)

func hello(val string) string { return "hello " + val } 

func main() {
    web.Get("/(.*)", hello)
    web.Run("0.0.0.0:9999")
}

To run the application, put the code in a file called hello.go and run:

8g hello.go && 8l -o hello hello.8 && ./hello

You can point your browser to http://localhost:9999/world .

Getting parameters

Route handlers may contain a pointer to web.Context as their first parameter. This variable serves many purposes -- it contains information about the request, and it provides methods to control the http connection. For instance, to iterate over the web parameters, either from the URL of a GET request, or the form data of a POST request, you can do the following:

package main

import (
    "web"
)

func hello(ctx *web.Context, val string) { 
    for k,v := range ctx.Params {
        println(k, v)
    }
}

func main() {
    web.Get("/(.*)", hello)
    web.Run("0.0.0.0:9999")
}

In this example, if you visit http://localhost:9999/?a=1&b=2, you'll see the following printed out in the terminal:

a 1
b 2

Documentation

For a quickstart guide, check out web.go's home page

There is also a tutorial

If you use web.go, I'd greatly appreciate a quick message about what you're building with it. This will help me get a sense of usage patterns, and helps me focus development efforts on features that people will actually use.

About

web.go was written by Michael Hoisie.

Follow me on Twitter!

Something went wrong with that request. Please try again.