URL Path To Array In Golang
Here’s a short Go function that converts the URL path into an array; it’s useful if you want to implement REST style queries into a simple application.
func parseComponents(r *http.Request) []string {
//Creates an App Engine Context - required to access App Engine specific services
c := appengine.NewContext(r)
//The URL that the user queried.
path := r.URL.Path
path = strings.TrimSpace(path)
//Log the URL received:
c.Infof("URL: ", path)
//Cut off the leading and trailing forward slashes, if they exist.
//This cuts off the leading forward slash.
if strings.HasPrefix(path, "/") {
path = path[1:]
}
//This cuts off the trailing forward slash.
if strings.HasSuffix(path, "/") {
cut_off_last_char_len := len(path) - 1
path = path[:cut_off_last_char_len]
}
//We need to isolate the individual components of the path.
components := strings.Split(path, "/")
return components
}
HTTP Basic Access Authorization In Golang
One of the simplest and oldest methods of authorization is HTTP Basic authorization. While it isn’t as secure as the Users service of App Engine or the OAuth authorization model, it’s easy to implement and looks visually impressive depending on the user’s browser.
Here’s a picture of the HTTP Basic authorization prompt in IE10 on Windows 8:
The variable authorization contains a base64 encoded hash generated by the user’s browser, created by concatenating the username and password together with a colon: username:password. R represents a http.Request reference, c is appengine.Context, and w is http.ResponseWriter.
//Get the authorization header.
authorization_array := r.Header["Authorization"]
if len(authorization_array) > 0 {
authorization := strings.TrimSpace(authorization_array[0])
c.Infof("Authorization: ", authorization)
} else {
w.Header().Set("WWW-Authenticate", "Basic realm=\"user\"")
http.Error(w, http.StatusText(401), 401)
}
Extract Subdomain From Request In Go
Configuring EdgeCache – A Cache For App Engine
Storing To The Memcache In Golang
A Simple index.yaml File
Sending Mail With Golang
Mapping Favicon.ico In A Golang Application
Golang Error: not enough arguments to return
One of the strengths of Go is the ability to return multiple values from a function. As a long-time Java programmer I’ve envied languages with muttiple returns for a long time. Here’s a quick summary of the feature:
Quick example: to return multiple values, use the classic return statement:
return value1, value2
And to get those return values from a function, you can write:
var1, var2 := someMultipleReturnFunction()
The great thing about this system is that you can ignore a return value if you don’t need it. For example, if you need to skip the first return value:
_, var2 := someMultipleReturnFunction()
Which brings me to the original reason for this blog post. I was writing a Go application, when a build failed due to the error not enough arguments to return. The problem was that I had multiple return statements within a function, and one of the statements declared only one return value, instead of the two return values that the function declared.
The easy fix? Simply declare nil to pad out the “missing” return value. So the return statement became:
return var1, nil
Easy, quick, and painless.