This repo is an example of how we might create an Microsoft Azure Function using Go language. Go lang doesn't have an existing template whilst using Azure Functions Core Tools. As such we need to use a "custom handler" to run Go lang applications either locally or in the cloud.
- Install the Azure Functions Core Tools.
- Install Go
- Clone this repo
-
Go to the root of this repo
-
Run
make run
to start the app: -
Click on
http://localhost:7071/api/func1
to browse the app
The application is enclosed in the func1 folder in hello.go. It uses a standard http.ServeMux
with a single registered route:
mux := http.NewServeMux()
route := fmt.Sprintf("/api/%s", getDirName())
mux.HandleFunc(route, helloHandler)
The host.json file in the root tells the Azure Function runtime the name and path of the binary to run:
"customHandler": {
"enableForwardingHttpRequest": true,
"description": {
"defaultExecutablePath": "out/func1",
"workingDirectory": "out",
"arguments": [
]
}
}
The function.json file in the func1
directory tells the Azure function runtime to use an httpTrigger
binding as the event to trigger any request/response for the func1
functions app:
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}