In this issue, I recommend a Go Web framework, Flamego.
Flamego is a simple, easily extensible, and modular Go Web framework. As the successor to the Macaron framework, Flamego has the most powerful routing configuration syntax in the Go language ecosystem today, and no Go Web framework can match it.
class=”pgc-h-arrow-right” data-track=”6″>
- Route configuration syntax unmatched in any Go Web framework
- Infinite route combination and nesting
- Inject middleware anywhere on the route
- Integrate non-invasively into any existing Web application
- Implement dependency injection through function signature to write code that is easier to test and maintain
class=”pgc-h-arrow-right” data-track=”40″>
Minimum Go version 1.16
go get github.com/flamego/flamego
class=”pgc-h-arrow-right” data-track=”41″> Start using
This example is only for users who are familiar with Go language and open in a new window, and have some understanding of HTTP and Web application development.
Let’s start with a very simple and ubiquitous example:
package main
import "github.com/flamego/flamego"
func main() {
f := flamego.Classic()
f.Get("/", func() string {
return "Hello, Flamego!"
})
f.Run()
}
On line 6 of the example, the function flamego.Classic creates and returns a classic Flame instance that integrates some default middleware, These include flamego.Logger, flamego.Recovery, and flamego.Static.
On line 7 of the example, calling the f.et method registers an anonymous function (lines 7-9) as the handler for the root path (“/”) when the GET request is received. In this case, the handler sends “Hello, Flamego!” to the client. String.
In line 10 of the example, we start the Web service by calling f.un. By default, Flame instances use 0.0.0.0:2830 as the listening address.
Next let’s run this sample code. We need to save the code to a local file and initialize a Go module:
$ mkdir flamego-example
$ cd flamego-example
$ nano main.go
$ go mod init flamego-example
go: creating new go.mod: module flamego-example
$ go mod tidy
go: finding module for package github.com/flamego/flamego
...
$ go run main.go
[Flamego] Listening on 0.0.0.0:2830 (development)
When you see the last row of logs appear, the Web service is ready!
We can do this by accessing the address http://localhost:2830 in a browser or using the curl command line tool:
$ curl http://localhost:2830
Hello, Flamego!
—END—
Open source protocol: MIT