If you just want to write a small website that you use, you probably don't need a framework, but if you are developing a website that goes into production, you will definitely need a framework and a good web framework.
If you already have all the necessary knowledge and experience, would you risk redeveloping all the features yourself? Do you have time to find a library that meets production-level requirements for development? Also, are you sure that this library will meet all your subsequent requirements?
These are the reasons why we are going to use the framework. Even the most cattle developers will not always want to rebuild the wheels. We can stand on the shoulders of our predecessors and go faster and better.
Introduction
Go is a fast-growing programming language designed to build simple, fast, and reliable software. Click here to see which good companies are using the Go language to drive their business.
This article will provide all the information necessary to help developers learn more about the best way to develop web applications using the Go language.
This article contains the most detailed framework comparisons, from the perspective of popularity, community support, and built-in features.
Beego: Open source, high performance Go language web framework.
Https://github.com/astaxie/beego
Https://beego.me
Buffalo: Quickly build web apps with the Go language.
Https://github.com/gobuffalo/buffalo
Https://gobuffalo.io
Echo: Simple, high-performance Go language web framework.
Https://github.com/labstack/echo
Https://echo.labstack.com
Gin: A web framework written in Go that implements APIs similar to the Martini framework with better performance.
Https://github.com/gin-gonic/gin
Https://gin-gonic.github.io/gin
Iris: The fastest Go language web framework in the universe. Complete MVC support, the future is under control.
Https://github.com/kataras/iris
Https://iris-go.com
Revel: An efficient, full-stack web framework for the Go language.
Https://github.com/revel/revel
Https://revel.github.io
Popularity
Rank by popularity (based on GitHub Star quantity)
https://github.com/speedwheel/awesome-go-web-frameworks/blob/master/README.md#popularity
learning curve
https://github.com/speedwheel/awesome-go-web-frameworks/blob/master/README.md#learning-curve
Astaxie and kataras have done a great job for Beego and Iris, and hope that other frameworks will catch up and provide more examples for developers. At least for me, if I want to switch to a new framework, those examples are the most abundant resources to get as much useful information as possible. An example is worth a thousand words.
Core functions
According to the number of features supported by the feature
https://github.com/speedwheel/awesome-go-web-frameworks/blob/master/README.md#core-features
Several well-known Go language Web frameworks are not really frameworks, that is: Echo, Gin, and Buffalo are not really Web frameworks (because they don't fully support all features) but most Go communities think they are So these frameworks can also be compared to Iris, Beego or Revel. Therefore, we are obliged to include these frameworks (Echo, Gin and Buffalo) in this table as well.
All of these frameworks, with the exception of Beego and Revel, can be adapted to any net/http middleware. Some of these frameworks can be easily adapted, others may require extra effort [even if the pain here is not certain].
Technical vocabulary
Routing: Named path parameters and wildcards
Can handle dynamic paths.
Example of a named path parameter:
"/user/{username}" matches "/user/me", "/user/speedwheel" etc.
The values of the path parameter username above are "me" and "speedwheel" respectively.
Examples of wildcards:
"/user/{path *wildcard}" matches
"/user/some/path/here",
"/user/this/is/a/dynamic/multi/level/path" etc.
The path parameters above correspond to "some/path/here" and "this/is/a/dynamic/multi/level/path" respectively.
Iris also supports a feature called macros, which can be expressed as /user/{username:string} or /user/{username:int min(1)}.
Routing: regular expression
Filter dynamic paths.
E.g:
"/user/{id ^[0-9]$}" matches "/user/42" but does not match "/user/somestring"
The value of the path parameter id here is 42.
Routing: grouping
Path groups with a common prefix are processed by shared logic or middleware.
E.g:
myGroup := Group("/user", userAuthenticationMiddleware)
myGroup.Handle("GET", "/", userHandler)
myGroup.Handle("GET", "/profile", userProfileHandler)
myGroup.Handle("GET", "/signup", getUserSignupForm)
/user
/user/profile
/user/signup
You can even create subgroups from a group:
myGroup.Group("/messages", optionalUserMessagesMiddleware)
myGroup.Handle("GET', "/{id}", getMessageByID)
/user/messages/{id}
Routing: All the above rules are combined without conflict
This is an advanced and useful feature, and many of us want routing modules or web frameworks to support this, but currently, only Iris can support this feature in the Go language framework.
This means that paths like /{path *wildcard} , /user/{username} and /user/static and /user/{path *wildcard} can pass static paths (/user/static) in the same route. Or the wildcard (/{path *wildcard}) to match correctly.
Routing: Custom HTTP error
Refers to the situation where the request error can be handled by itself. The error status code of Http is >=400. For example, NotFound 404, the requested resource does not exist.
E.g:
OnErrorCode(404, myNotFoundHandler)
Most of the above web frameworks only support the handling of 404, 405 and 500 error states, but frameworks such as Iris, Beego and Revel are fully supported for HTTP error status codes and even support any error. ( any error -- any error, only Iris can support).
100% compatible with net/http
this means:
These frameworks allow you to get all the relevant information for *http.Request and http.ResponseWriter directly.
Each framework provides a way to handle the net/http request accordingly.
Middleware ecosystem
The framework will provide you with a complete engine to define the process, global, single or set of routes without having to wrap each part of the processor with a different middleware. The framework provides functions such as Use, Middleware, and Done.
API design for class Sinatra (Translator's Note: Sinatra is a domain-specific language based on Ruby)
You can inject code into the runtime to handle specific HTTP methods (and path parameters).
E.g:
.Get or GET("/path", gethandler)
.Post or POST("/path", postHandler)
.Put or PUT("/path", putHandler) and etc.
Server program: HTTPS is enabled by default
The framework's server supports registration and automatic update of SSL certificates to manage new incoming SSL/TLS connections (https). The most famous default https enabled provider is letsencrypt.
Server program: Gracefully Shutdown
When you press CTRL + C to close your terminal application, the server will wait (a certain wait time) for other connections to complete related tasks or trigger a custom event to do the cleanup (eg close the database), and finally stop smoothly service.
Server program: multiple listeners
The framework's server supports a custom net.Listener or can launch a web application with multiple http services and addresses.
Full support for HTTP/2
The framework supports the HTTP/2 protocol for https requests and supports the Server Push feature.
Subdomain
You can inject the path to the subdomain directly in your web app.
Secondary means that this feature is not natively supported by this framework, but you can still do so by enabling multiple http servers. The disadvantage of this is that the main program and the subdomain program are not connected. By default, they cannot share logic.
Session (Sessions)
Support for http sessions and the use of sessions in custom handlers.
Some web frameworks support a back-end database to store sessions so that persistent sessions are still available after the server is restarted.
Buffalo uses gorilla's sessions library, which is slightly slower than other framework implementations.
E.g:
Func setValue(context http_context){
s := Sessions.New(http_context)
s.Set("key", "my value")
}
Func getValue(context http_context){
s := Sessions.New(http_context)
myValue := s.Get("key")
}
Func logoutHandler(context http_context){
Sessions.Destroy(http_context)
}
Wiki: https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#HTTP_session
Network sockets (Websockets)
The framework supports the websocket communication protocol. Different frameworks have different implementations for this.
You should use their examples to see which one is right for you. One of my colleagues, after trying the websocket functionality in all of the above frameworks, told me that Iris implements the most websocket features and provides a relatively easy to use API.
Wiki: https://en.wikipedia.org/wiki/WebSocket
Program inline support for views (aka templates)
Normally, you must convert the template files one by one according to the executables of the web application. Embedding into the application means that the framework integrates go-bindata, so the template can be included in the final executable as []byte.
What is a view engine?
The framework supports template loading, customization, and in-house modeling features to save us development time.
View Engine: STD
The framework supports the standard html/template parser loading template.
View Engine: Pug
Framework support Pug parser loading
stencil.
View engine: Django
The framework supports the Django parser loading template.
View Engine: Handlebars
The framework supports the Handlebars parser loading template.
View engine: Amber
The framework supports the Amber parser loading template.
Rendering: Markdown, JSON, JSONP, XML...
The framework provides an easy way to send and customize responses to various content types.
MVC
The Model–view–controller (MVC) model is a software architecture pattern for implementing a user interface on a computer, which divides an application into three parts that are related to each other. The purpose of this is to separate the internal processing logic of the information, the information to the user, and the information obtained from the user. The MVC design pattern decouples these three components for efficient code reuse and parallel development.
Iris supports full MVC functionality and can be injected at runtime.
Beego only supports matching of methods and data models and can be injected at runtime.
Revel supports methods, matching of paths and data models, which can only be injected through the generator (the generator is another different piece of software used to build your web application).
Wiki: https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
Cache
Web caching (or http caching) is a way to temporarily store (cache) web documents, such as HTML pages and images, to slow down server latency. A web caching system caches web documents so that subsequent requests can directly obtain cached documents if certain conditions are met. A web caching system can refer to both a device and a software program.
Wiki: https://en.wikipedia.org/wiki/Web_cache
file server
You can register a (physical) directory to a path so that files under this path can be automatically provided to the client.
File server: embedded application
Normally, you must transfer all static files (such as static assets, assets: CSS, JavaScript files, etc.) along with the application's executable. The framework that supports this feature gives you the opportunity to embed all of this data in the form of []byte in your application. Because the server can use this data directly without having to find files in physical locations, they will be more responsive.
The response can be modified multiple times during the life cycle before sending
Currently only Iris supports this feature through the response writer built into http_context.
When the framework supports this feature, you can retrieve, reset, or modify the status code, body, and headers before returning to the client. By default, this is not possible in a net/http-based web framework because the body and status codes cannot be retrieved or modified as soon as they are written.
Gzip
When you are in a routing handler and you can change the response writer to send a gzip-compressed response, the framework is responsible for the response header. If any errors occur, the framework should reset the response to normal, and the framework should be able to check if the client supports gzip compression.
Gzip is a file format and software program for compression and decompression.
Wiki: https://en.wikipedia.org/wiki/Gzip
Test framework
You can use framework-specific libraries to help you easily write better test code to test your HTTP.
For example (currently only Iris supports this feature):
Func TestAPI(t *testing.T) {
App := myIrisApp()
Tt := httptest.New(t, app)
tt.GET("/admin").WithBasicAuth("name", "pass").Expect().
Status(httptest.StatusOK).Body().Equal("welcome")
}
myIrisApp returns your fictitious web application with a GET method for the /admin path, which has basic authentication logic protection.
For the simple test above, authenticate with "name" and "pass" and access GET /admin to check if its response status is Status OK and the body of the response is "welcome".
TypeScript Translator
The goal of TypeScript is to become a superset of ES6. In addition to all the new features defined by the standard, it adds a static type system. TypeScript also has converters for converting TypeScript code (ie ES6 + types) to ES5 or ES3 JavaScript code so that we can run it in today's browsers.
Online editor
With the help of an online editor, you can compile and run code online quickly and easily.
Log system
Custom log systems extend native log packages by providing useful features such as color log output, formatting, log level separation, and different logging backends.
Maintenance and automatic updates
Users who notify the framework are updated in a non-intrusive manner.