Fiber released v1.7! 🎉 What's new and is it still fast, flexible and friendly?
Tags: go, node, news, fiber
Published: 2020-02-21 15:33:30

## Introduction

Good Friday, everybody! 👋  It's a great day for upgrades and even more so for good ones. So, I'd like to suggest you go to a new level of **Fiber** Go web framework — new version `v1.7`.

> 💭 Please note: official docs may _not_ contains _extensive_ description for some changes. **Don't worry**, authors are working right now for it.

### 📝 Table of contents

- [Official Fiber logo](#ch-0)
- [New features](#ch-1)
  - [Render() method](#ch-1-1)
  - [Error() method and re-new Next(err)](#ch-1-2)
  - [BodyParser() method](#ch-1-3)
  - [Group() method](#ch-1-4)
  - [WebSocket() method](#ch-1-5)
  - [Recover() method](#ch-1-6)
  - [Map() method](#ch-1-7)
- [Updates & Improvements](#ch-2)
  - [Re-new HTTP methods](#ch-2-1)
  - [Re-think Settings method](#ch-2-2)
- [Deprecated & Removed list](#ch-3)
- [Benchmarks](#ch-4)
- [Do you like Fiber?](#ch-5)
- [Your assistance to project](#ch-6)

<a name="ch-0"></a>
## Official Fiber logo

First of all, **Fiber** `v1.7` have a new awesome logo:

![Fiber v1.7 logo](https://dev-to-uploads.s3.amazonaws.com/i/pow7zgdeng298u9tgedn.png)

> Thanks to _Rasmus Andersen_ for great open source font called [Inter](https://rsms.me/inter/) 🥰

<a name="ch-1"></a>
## New features

OK. Let's go to another new features! 👇

<a name="ch-1-1"></a>
### ✅ Render() method

Add support for template engines:

- [html](https://golang.org/pkg/html/template/)
- [amber](https://github.com/eknkc/amber)
- [handlebars](https://github.com/aymerick/raymond)
- [mustache](https://github.com/cbroglie/mustache)
- [pug](https://github.com/Joker/jade)

You can setup template engine before initiation app:

```go
app := fiber.New(&fiber.Settings{
  TemplateEngine:    "mustache",
  TemplateFolder:    "./views",
  TemplateExtension: ".tmpl",
})
```

Or after initiation:

```go
app.Settings.TemplateEngine = "mustache"
app.Settings.TemplateFolder = "./views"
app.Settings.TemplateExtension = ".tmpl"
```

And now, you can call template `./views/home.tmpl` like this:

```go
app.Get("/", func(c *fiber.Ctx) {
  c.Render("home", fiber.Map{
    "title": "Homepage",
    "year":  1999,
  })
})
```

<a name="ch-1-2"></a>
### ✅ Error() and Next(err) methods

Now, you may contains the [Error](https://docs.gofiber.io/api/ctx#error) information, that thrown by a `panic` or passed via the [Next(err)](https://docs.gofiber.io/api/ctx#next) method:

```go
app.Get("/api/user", func (c *fiber.Ctx) {
  if err := c.JSON(&User); err != nil {
    c.Next(err) 
    // => if something went wrong here, we can handle it
  }
})

app.Use("/api", func(c *fiber.Ctx) {
  c.Set("Content-Type", "application/json")
  c.Status(500).Send(c.Error())
})
```

<a name="ch-1-3"></a>
### ✅ BodyParser() method

Binds the request body to a struct:

```go
// curl -X POST -H "Content-Type: application/json" \ 
//   --data '{"name":"john","pass":"doe"}' localhost:3000

// curl -X POST -H "Content-Type: application/xml" \ 
//   --data '<Login><name>john</name><pass>doe</pass><Login>' localhost:3000

// curl -X POST -H "Content-Type: application/x-www-form-urlencoded" \
//   --data 'name=john&pass=doe' localhost:3000

// curl -v -F name=john -F pass=doe localhost:3000

type Person struct {
  Name string `json:"name" xml:"name" form:"name"`
  Pass string `json:"pass" xml:"pass" form:"pass"`
}

app.Post("/", func(c *fiber.Ctx) {
  var person Person

  if err := c.BodyParser(&person); err != nil {
      // Handle error
  }

  // Do something with person.Name or person.Pass
})
```

<a name="ch-1-4"></a>
### ✅ Group() method

Add chain function for [groupping](https://docs.gofiber.io/api/app#group) routes:

```go
api := app.Group("/api", cors())  // /api

v1 := api.Group("/v1", mysql())   // /api/v1
v1.Get("/list", handler)          // /api/v1/list
v1.Get("/user", handler)          // /api/v1/user

v2 := api.Group("/v2", mongodb()) // /api/v2
v2.Get("/list", handler)          // /api/v2/list
v2.Get("/user", handler)          // /api/v2/user
```

<a name="ch-1-5"></a>
### ✅ WebSocket() method

Add [WebSocket]() support using [gorilla *Conn](https://github.com/gofiber/recipes/blob/master/websocket/main.go):

```go
app := fiber.New()

app.WebSocket("/ws/:name", func(c *fiber.Conn) {
  log.Println(c.Params("name"))

  for {
    mt, msg, err := c.ReadMessage()
    if err != nil {
      log.Println("read:", err)
      break
    }

    log.Printf("recovery: %s", msg)

    err = c.WriteMessage(mt, msg)
    if err != nil {
      log.Println("write:", err)
      break
    }
  }
})

// ws://localhost:3000/ws/john
app.Listen(3000)
```

<a name="ch-1-6"></a>
### ✅ Recover() method

[Recover](https://docs.gofiber.io/api/middleware/recover) from `panic`:

```go
app.Get("/", func(c *fiber.Ctx) {
  panic("Something went wrong!") // it's panic time!
})

app.Recover(func(c *fiber.Ctx) {
  c.Status(500).Send(c.Error())
  // => 500 "Something went wrong!"
})
```

<a name="ch-1-7"></a>
### ✅ Map() method

Add shortcut for `map[string]interface{}`:

```go
fiber.Map{
  "first_name": "John",
  "is_admin":   true,
}
```

![Go build](https://dev-to-uploads.s3.amazonaws.com/i/a8m3e4hiz93ty83smi6s.jpg)

<a name="ch-2"></a>
## Updates & Improvements

And here are updates and improvements, that **Fiber** has prepared for you! 👇

<a name="ch-2-1"></a>
### ℹ️ Re-new HTTP methods

Add multiple middlewares/handlers inside [routes](https://docs.gofiber.io/guide/routing) & [groups](https://docs.gofiber.io/guide/grouping):

```go
app.Get("/", middlewareOne(), middlewareTwo(), handler())
```

<a name="ch-2-2"></a>
### 🔀 Re-think Settings method

Pass optional [settings](https://docs.gofiber.io/api/fiber#config) before app initiation, inside [New()](https://docs.gofiber.io/api/fiber#new) method:

```go
app := fiber.New(&fiber.Settings{
  Prefork:       true,
  CaseSensitive: true,
  StrictRouting: true,
  ServerHeader:  "Go Server",
  // ...other settings
})
```

<a name="ch-3"></a>
## Deprecated & Removed list

Follow functions are _deprecated_ and _removed_ from **Fiber** `v1.7`:

🚫 ~~app.Banner~~
🚫 ~~ctx.BasicAuth~~
🚫 ~~ctx.Json~~
🚫 ~~ctx.JsonBytes~~
🚫 ~~ctx.JsonString~~
🚫 ~~ctx.Xml~~

<a name="ch-4"></a>
## 🤖 Benchmarks

### TechEmpower

![TechEmpower](https://dev-to-uploads.s3.amazonaws.com/i/pbd5j496uf1g1n1s86h8.png)

All results: https://www.techempower.com/benchmarks/#section=data-r19&hw=ph&test=composite&a=2

### The Benchmarker

|#|Language|Framework|Speed (64)|Speed (256)|Speed (512)|Speed (1024)|Speed (2048)|
|-|-|-|-|-|-|-|-|
|6|go (1.13)|fiber (1.7)|137902|147913|149225|143516|143006|

All results: https://github.com/the-benchmarker/web-frameworks

<a name="ch-5"></a>
## 💬 Do you like Fiber? Tell about it!

Fiber authors are **always** listening to its users in [issues](https://github.com/gofiber/fiber/issues) and all over the Internet. Therefore, it would be great, if you could share your opinion or/and experience with **Fiber** to authors in [GitHub](https://github.com/gofiber/fiber) repository!

> [...] because it's only right way to create a **fast**, **flexible** and **friendly** Go web framework for any tasks, deadlines and developer skills!
>
> _— Fiber Authors_

![Do you like Fiber?](https://dev-to-uploads.s3.amazonaws.com/i/v22vk8sjujiixm7i1jfg.jpg)

<a name="ch-6"></a>
## Your assistance to project 👍

1. Add a [GitHub Star](https://github.com/gofiber/fiber) to project.
2. Tweet about Fiber [on your Twitter](https://twitter.com/intent/tweet?text=%F0%9F%9A%80%20Fiber%20is%20an%20Express%20inspired%20web%20framework%20written%20in%20%23Go%20with%20%F0%9F%92%96%20https%3A%2F%2Fgithub.com%2Fgofiber%2Ffiber).
3. Help to translate `README` and [API Docs](https://docs.gofiber.io/) to another language (_at this moment, Fiber was translated to **10** languages_).

## Photo by

[Title, 1] Vic Shóstak https://github.com/koddr
[2, 3] Ashley McNamara https://github.com/ashleymcnamara/gophers

<a name=""></a>
## P.S.

If you want more articles (like this) on this blog, then post a comment below and subscribe to me. Thanks! 😻

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 projects that need your help (and stars) 👇

- 🔥 [gowebly][gowebly_url]: A next-generation CLI tool for easily build amazing web applications with Go on the backend, using htmx & hyperscript and the most popular atomic/utility-first CSS frameworks on the frontend.
- ✨ [create-go-app][cgapp_url]: Create a new production-ready project with Go backend, frontend and deploy automation by running one CLI command.
- 🏃 [yatr][yatr_url]: Yet Another Task Runner allows you to organize and automate your routine operations that you normally do in Makefile (or else) for each project.
- 📚 [gosl][gosl_url]: The Go Snippet Library provides snippets collection for working with routine operations in your Go programs with a super user-friendly API and the most efficient performance.
- 🏄‍♂️ [csv2api][csv2api_url]: The parser reads the CSV file with the raw data, filters the records, identifies fields to be changed, and sends a request to update the data to the specified endpoint of your REST API.
- 🚴 [json2csv][json2csv_url]: The parser can read given folder with JSON files, filtering and qualifying input data with intent & stop words dictionaries and save results to CSV files by given chunk size.

<!-- Footer links -->

[gowebly_url]: https://github.com/gowebly
[cgapp_url]: https://github.com/create-go-app
[yatr_url]: https://github.com/koddr/yatr
[gosl_url]: https://github.com/koddr/gosl
[csv2api_url]: https://github.com/koddr/csv2api
[json2csv_url]: https://github.com/koddr/json2csv