Golang Users API Example

Here’s an example of how to use the Users API in Go. This example checks to see if the current user is logged in; if not, the user is given a link to log in. If the user is already logged in, then it creates and prints a link to log out.

//Creates an App Engine Context - required to access App Engine services.
c := appengine.NewContext(r)
//Acquire the current user
user := appengineuser.Current(c)
if user == nil {
    url, _ := appengineuser.LoginURL(c, "/")
    fmt.Fprintf(w, `<a href="%s">Sign in</a>`, url)
} else {
    url, _ := appengineuser.LogoutURL(c, "/")
    fmt.Fprintf(w, `Welcome, %s! (<a href="%s">sign out</a>)`, user, url)
}

Aliasing Imports In Golang

A quick note: Go allows the aliasing of imports. For instance, the below line of code imports the appengine/datastore package under the name aedatastore :

import aedatastore "appengine/datastore"

Then the application can call datastore services under the alias name:

aedatastore.Get(c, key, &entity)

This aliasing is particularly useful when package names collide. Assume that an application needs to import the Go image and appengine/image packages. Since both packages are named image, the Go compiler will issue an image redeclared as package name error if both are imported. To avoid this, alias one of the imports:

import "image"
import aeimage "appengine/image"

Retrieving The User’s IP Address

Retrieving the originating IP address of the request is simple in most languages.

Here’s how to retrieve the IP address in Java ( req represents a javax.servlet.http.HttpServletRequestreference ):

String ip = req.getRemoteAddr();

Here’s the same line in Go ( r represents http.Request ):

ip := r.RemoteAddr

In PHP, the originating IP can be retrieved from the predefined variable SERVER :

$_SERVER['REMOTE_ADDR']

Handling Form Data In Go

Here’s how to retrieve the value of a form element named comment ( r represents http.Request ):

comment := r.FormValue("comment")

Suppose the form value is an integer. Here’s how to extract the value from the request and convert it from a string to an integer (if an error occurs when converting, the value -1 is stored):

index, err := strconv.Atoi(r.FormValue("index"))    
if err != nil {
    index = -1
}

If needed, the submitted form value can be written to logging by calling:

c := appengine.NewContext(r)
c.Infof("Submitted Value: %v", index)

Templating In Go

The Go language contains an extremely powerful templating engine which simplifies the creation of web pages. Here’s a quick overview of how it works.

First of all, create a set of template HTML files. Wherever dynamic content should be added, add a {{.name-of-property}} annotation. Here’s an example:

<div class="row">
    <div class="span12">{{.propertyone}}</div>
    <div class="span8">{{.propertytwo}}</div>
</div>

In the above example, propertyone and propertytwo represent keys which will be replaced by values later on.

Next, make these template files available to the Go application. This line of code pulls in an entire folder of template files (assuming that templates is a root level directory):

var templates, templates_err = template.ParseGlob("templates/*")

Alternately, a set of templates can be called in by name:

var templates = template.Must(template.ParseFiles("templates/one.html", "templates/two.html", "templates/three.html"))

Using template.Must ensures that all the templates files are loaded in – if it is unable to find a named template file, template.Must sets off a panic.

Next, create a map of strings to display on the page. We associate the keys (which were set within the HTML of the template file) with values that will replace the annotations:

//This map holds the data to display on the page.
display_data := make(map[string]string)
display_data["propertyone"] = string_one
display_data["propertytwo"] = string_two
display_data["propertythree"] = string_three

Finally, execute the template. W represents a http.ResponseWriter reference to write the completed template to, one.html represents the name of the template to use, and display_data is the map that defines the values to use when replacing the annotations:

//Put the data into the template and send to the user.
templates.ExecuteTemplate(w, "one.html", display_data)

Remember to import the html/template package before using this code.

Returning Status Codes In Golang

Here’s an overview of how to send HTTP status codes in Go. W represents a http.ResponseWriterreference, and r is a http.Request reference.

The following line tells the browser to redirect to the URL given (a 301 is a Moved Permanently redirect):

http.Redirect(w, r, "http://learntogoogleit.com/", 301)

Here is a standard Not Found error:

http.Error(w, http.StatusText(404), 404)

An Internal Server Error is indicated using a 500 status code:

http.Error(w, http.StatusText(500), 500)

The above examples use StatusText to retrieve standard error text, but you can set your own text if needed:

http.Error(w, "Too Many Requests", 429)

Error Parsing YAML File: While Scanning A Simple Key

App Engine uses the app.yaml file to route incoming requests to the appropriate handlers. It’s important to write proper YAML code in this file, otherwise your application may behave erratically or not at all.

One common problem with YAML files is failing to properly separate key:value pairs. The YAML specification requires a colon ( : ) and one space character between the key and the associated value. Here’s an example of a properly formatted YAML key:value pair:

Key: Value

Now here’s an example of a broken app.yaml file:

application: an-example-application-id
version: 1
runtime: php
api_version: 1
threadsafe:true

Notice the error? The threadsafe property has a colon, but no space separating the key ( threadsafe) and the value ( true ). Here’s a screenshot of appcfg refusing to upload this broken file:

If you receive this error, make sure that all of your YAML properties are separated by a colon and a space. One space is enough, don’t use tabs or multiple spaces.

Static File Referenced By Handler Not Found

The error static file referenced by handler not found is usually caused by an error in an application’s app.yaml. Basically, it means that one of the static file handlers in app.yaml is redirecting to a file that doesn’t exist or is named incorrectly.

Here’s a simple example. Suppose an application maps favicon.ico in this manner:

- url: /favicon.ico
  static_files: static/favicon.ico
  upload: static/favicon.ico

This handler statement says that the application has a folder named static, which holds a file named favicon.ico. But it maps the file so it looks like it’s located at the root of the application: example-id . appspot . com / favicon.ico. Now if the folder static doesn’t exist, or the file is missing, then attempting to access the file via the web will cause this error. Here’s how it looks in App Engine logs:

To fix, review the handlers section of app.yaml and make sure that the referenced files exist within the application folder.

Querying The Datastore In Golang

Here’s a demonstration of how to query the datastore in Go.

In this example we filter on PropertyOne, requiring it to be equal to true. You can also set other inequalities such as greater than ( > ). Kind is the kind of the entities to query, and PropertyTwodemonstrates ordering by descending order. CustomStruct is the struct that was used to create the entity. Remember to put your entity processing code just before the last brace ( } ).

//Search the datastore for entities
q := datastore.NewQuery(kind).Filter("PropertyOne =", true).Order("-PropertyTwo")
//Loop through each returned entity.
for t := q.Run(c); ; {
    //This represents the entity currently being processed.
    var x CustomStruct
    key, err := t.Next(&x)
    if err == datastore.Done {
        //This "error" means that we're done going through entities
        //Since we're done, break out of this loop.
        break
    }
    if err != nil {
        //Some other error happened. Report error and panic.
        c.Infof("Error in querying datastore: %v", err)
        panic(err)
    }
    //DO SOMETHING HERE WITH ENTITY x
}//end for going through q.Run

Deleting An Entity In Golang

Here’s a short code example demonstrating how to delete an entity in Go. C is an appengine.Contextreference, kind is the entity kind, and entity_id_int represents the integer ID of the entity.

key := datastore.NewKey(c, kind, "", entity_id_int, nil)
datastore.Delete(c, key)