Go

Supports Go1.x, that means it updates to latest version of Go.

Function declaration

In Go you can declare your function in small or big case. The usual big case are accessible throughout modules. A Public function needs to satify following conditions:-

  1. Should use package main at the top.

  2. Function name in Capital Case.

  3. Return type must be string, error.

  4. string return must be encapsulated inside Response struct; and called with method send().

  5. The above function needs be declared in router.go as final step.

hello.go
package main//step-1

func FunctionName(req Request)(string, error){ //step-2,3
	r := &Response{"some string", nil} //step-4
	return r.send()
}

and router.go as;

router.go
package main

var Router = map[string]func(Request) (string, error){
    "FunctionName": FunctionName,
}

NOTE: , needs to be present after declaring function. (Important step)

Request is a type that is shown as;

type Request struct {
	Path string                 `json:"path"`
	Args map[string]interface{} `json:"args"`
}

Args is a string to interface map and has following keys.

method, path, body, headers, query

can retrieve information about request from here.

Restrictions

  • Module name cannot be func.go.

  • Returns json with keys data, error.

Example

Hello World in Go.

main.go
package main

func Hello(req Request)(string, error){ 
	r := &Response{"Hello from GO!", nil}
	return r.send()
}
router.go
package main

var Router = map[string]func(Request) (string, error){
    "Hello": Hello,
}

This returns response as json;

{"data":"Hello from GO","error":null}

Last updated