Welcome to Fiber β an Express.js styled web framework written in Go with οΈβ€οΈ
Vic ShΓ³stak
Posted on February 3, 2020
Introduction
Hello, World! π Today we will deal with a young (but ambitious) Fiber web framework on Go and understand that this is not "yet another new framework, like X", but a great tool for creating rapid web applications.
π It will be a review article, dive into Fiber we will start further.
Table of contents
- What is Fiber and why is it so good?
- Useful information to start working with Fiber
- Main features
- Benchmarks
- Project assistance
What is Fiber and why is it so good?
Follow official README from GitHub repository:
Fiber is an Express.js styled HTTP web framework implementation running on Fasthttp, the fastest HTTP engine for Go (Golang). The package make use of similar framework convention as they are in Express.
And I tend to agree with that. If you have ever implemented a web application on Node.js using Express.js (like me), then many methods and principles will seem very common to you!
For example, this is standard Hello, World!
by Express.js:
// ...
const app = express()
app.get('/', (req, res) => res.send('Hello, World!'))
app.listen(8080)
And similar example by Fiber:
// ...
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) {
c.Send("Hello, World!")
})
app.Listen(8080)
Useful information to start working with Fiber
Actually, all you need for start is official documentation! π
Fiber, as a web framework, was created with idea of minimalism to more easily start creating a web application's backend for new gophers, but who have experience with JavaScript.
That's what the authors themselves say:
People switching from Node.js to Go often end up in a bad learning curve to start building their webapps, this project is meant to ease things up for fast development, but with zero memory allocation and performance in mind.
Main features
- Optimized for speed and low memory usage
- Rapid Server-Side Programming
- Easy routing with parameters
- Static files with custom prefix
- Middleware with
Next()
support - Express API endpoints
- Extended documentation
Easy to enable the prefork feature
Just set Prefork
to true
on your code:
// ...
app := fiber.New()
app.Prefork = true // enable prefork
app.Get("/", func(c *fiber.Ctx) {
c.Send(fmt.Sprintf("Hi, I'm worker #%v", os.Getpid()))
// => Hi, I'm worker #16858
// => Hi, I'm worker #16877
// => Hi, I'm worker #16895
})
app.Listen(8080)
What's prefork?
Enable prefork feature will spawn multiple go processes listening on the same port. Nginx has a great article about Socket Sharding, this picture are taken from the same article π
β¨ My favorite killer feature β¨
And one more big feature (for me) is full access to all Fasthttp methods and properties (read documentation for more info about it).
Yeah, you didn't mishear! Fiber is extremely easy to use as Express.js and has everything under the hood that Fasthttp has now and will have in the future π₯
Benchmarks π€
Click here to see all benchmark results. I'll only bring some.
- TechEmpower: JSON serialization
- Go-Web: enable HTTP pipelining
Project assistance
- Add a GitHub Star to project.
- Tweet about project on your Twitter.
- Help to translate README and API Docs to another language.
Photo by
[Title] Fiber Authors https://gofiber.io/
[1] Nate Grant https://unsplash.com/photos/dFF8z3WH5FI
P.S.
If you want more articles (like this) on this blog, then post a comment below and subscribe to me. Thanks! π»
βοΈ You can support me on Boosty, both on a permanent and on a one-time basis. All proceeds from this way will go to support my OSS projects and will energize me to create new products and articles for the community.
And of course, you can help me make developers' lives even better! Just connect to one of my projects as a contributor. It's easy!
My main projects that need your help (and stars) π
- π₯ gowebly: A next-generation CLI tool that makes it easy to create amazing web applications with Go on the backend, using htmx, hyperscript or Alpine.js and the most popular CSS frameworks on the frontend.
- β¨ create-go-app: Create a new production-ready project with Go backend, frontend and deploy automation by running one CLI command.
Posted on February 3, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.