Guides

Query(key string, index ...int) (string, error)

Query method retrieves the form data, returns an empty string and the error if not found.

index is an optional integer field, by indicating the index parameter Golf will take the query value of key as an array.

Param(key string) (string)

Param method retrieves the parameters from URL.
For example, if the route for the request handler is /:id/, then id can be retrieved by calling ctx.Param(id)

The following code snippet is a example usage of ctx.Param.

package main

import "github.com/dinever/golf"

func pageHandler(ctx *golf.Context) {
  ctx.Write("Page: " + ctx.Param("page"))
}

func main() {
  app := golf.New()
  app.Get("/p/:page/", pageHandler)
  app.Run(":9000")
}

ClientIP() string

ClientIP implements a best effort algorithm to return the real client IP, it parses X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: Nginx or HAProxy.

Redirect(url string)

Redirect redirects the request to the location indicated by the url parameter. It works by setting a Location key in the response header.

If you need a 302 redirection, please do it by setting the Header manually.

Cookie(key string) (string, error)

Cookie returns the value of the cookie by indicating the key.

SetCookie(key string, value string, expire int)

SetCookie sets cookies for the request by indicating key and value. The parameter expire indicates the lifetime(seconds) of the cookie in the browser.

If expire is 0, SetCookies creates a session cookie, which means the cookie will expire immediately when the user closes the browser.

JSON(obj interface{})

JSON returns a JSON response. Any instances that can be encoded by the jsonMarshal` method provided by the Go standard library can be passed into this method.

StatusCode() int

StatusCode returns the status code that golf has sent.

SendStatus(statusCode int)

Sets the response HTTP status code to statusCode and send its string representation as the response body.

func handler(ctx *golf.Context) {
  ctx.SendStatus(404)
  ctx.Send("Content not found here")
}

Send(content interface{})

Send sends the content passed to it immediately to the client and sets the field Context.IsSent as true to prevent response being sent twice for a context.

The type that Context.Send accepts are string, []byte and *bytes.Buffer.

Send a *bytes.buffer object:

func sendBufferHandler(ctx *Context) {
	var b bytes.Buffer
	b.Write([]byte("Hello "))
  b.Write([]byte("World!"))
	ctx.Send(&b)
}

Send a byte array:

func sendByteArrayHandler(ctx *Context) {
	ctx.Send([]byte("Hello World!"))
}

Send a string:

func sendStringHandler(ctx *Context) {
	ctx.Send("Hello World!")
}

SetHeader(key, value string)

SetHeader sets the header entries associated with key to the single element value. It replaces any existing values associated with key.

AddHeader(key, value string)

AddHeader adds the key, value pair to the header. It appends to any existing values associated with key.

Abort(statusCode int, data... map[string]interface{})

Abort returns an HTTP Error by indicating the status code, the corresponding handler inside App.errorHandler will be called. Otherwise, the defaultErrorHandler will be called If the user has not set the corresponding error handler.

You can also pass data into this method, and data will be passed to the corresponding error handler. The data passed into error handlers can be used in error template rendering.

🚧

Warning

The request handler must be returned manually after calling Error. Otherwise the following code will still be executed and may cause unintended behavior.

Render(file string, data interface{})

Render renders the indicated template file using data as variables and sets the response body as the rendered result.

RenderFromString(tplSrc string, data interface{})

RenderFromString takes the string parameter tplSrc as the template source, renders variables data into the string template and sets the response body as the rendered result.