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:-
Should use
package main
at the top.Function name in Capital Case.
Return type must be
string, error
.string
return must be encapsulated insideResponse
struct; and called with methodsend()
.The above function needs be declared in
router.go
as final step.
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;
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 keysdata, error
.
Example
Hello World in Go.
package main
func Hello
(req Request)(string, error){
r := &Response{"Hello from GO!", nil}
return r.send()
}
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