github.com/gofiber/fiber/v2@v2.47.0/docs/intro.md (about)

     1  ---
     2  slug: /
     3  id: welcome
     4  title: 👋 Welcome
     5  sidebar_position: 1
     6  ---
     7  
     8  An online API documentation with examples so you can start building web apps with Fiber right away!
     9  
    10  **Fiber** is an [Express](https://github.com/expressjs/express) inspired **web framework** built on top of [Fasthttp](https://github.com/valyala/fasthttp), the **fastest** HTTP engine for [Go](https://go.dev/doc/). Designed to **ease** things up for **fast** development with **zero memory allocation** and **performance** in mind.
    11  
    12  These docs are for **Fiber v2**, which was released on **September 15th, 2020**.
    13  
    14  ### Installation
    15  
    16  First of all, [download](https://go.dev/dl/) and install Go. `1.17` or higher is required.
    17  
    18  Installation is done using the [`go get`](https://pkg.go.dev/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) command:
    19  
    20  ```bash
    21  go get github.com/gofiber/fiber/v2
    22  ```
    23  
    24  ### Zero Allocation
    25  Some values returned from \***fiber.Ctx** are **not** immutable by default.
    26  
    27  Because fiber is optimized for **high-performance**, values returned from **fiber.Ctx** are **not** immutable by default and **will** be re-used across requests. As a rule of thumb, you **must** only use context values within the handler, and you **must not** keep any references. As soon as you return from the handler, any values you have obtained from the context will be re-used in future requests and will change below your feet. Here is an example:
    28  
    29  ```go
    30  func handler(c *fiber.Ctx) error {
    31      // Variable is only valid within this handler
    32      result := c.Params("foo") 
    33  
    34      // ...
    35  }
    36  ```
    37  
    38  If you need to persist such values outside the handler, make copies of their **underlying buffer** using the [copy](https://pkg.go.dev/builtin/#copy) builtin. Here is an example for persisting a string:
    39  
    40  ```go
    41  func handler(c *fiber.Ctx) error {
    42      // Variable is only valid within this handler
    43      result := c.Params("foo")
    44  
    45      // Make a copy
    46      buffer := make([]byte, len(result))
    47      copy(buffer, result)
    48      resultCopy := string(buffer) 
    49      // Variable is now valid forever
    50  
    51      // ...
    52  }
    53  ```
    54  
    55  We created a custom `CopyString` function that does the above and is available under [gofiber/utils](https://github.com/gofiber/fiber/tree/master/utils).
    56  
    57  ```go
    58  app.Get("/:foo", func(c *fiber.Ctx) error {
    59  	// Variable is now immutable
    60  	result := utils.CopyString(c.Params("foo")) 
    61  
    62  	// ...
    63  })
    64  ```
    65  
    66  Alternatively, you can also use the `Immutable` setting. It will make all values returned from the context immutable, allowing you to persist them anywhere. Of course, this comes at the cost of performance.
    67  
    68  ```go
    69  app := fiber.New(fiber.Config{
    70  	Immutable: true,
    71  })
    72  ```
    73  
    74  For more information, please check [**\#426**](https://github.com/gofiber/fiber/issues/426) and [**\#185**](https://github.com/gofiber/fiber/issues/185).
    75  
    76  ### Hello, World!
    77  
    78  Embedded below is essentially the most straightforward **Fiber** app you can create:
    79  
    80  ```go
    81  package main
    82  
    83  import "github.com/gofiber/fiber/v2"
    84  
    85  func main() {
    86  	app := fiber.New()
    87  
    88  	app.Get("/", func(c *fiber.Ctx) error {
    89  		return c.SendString("Hello, World!")
    90  	})
    91  
    92  	app.Listen(":3000")
    93  }
    94  ```
    95  
    96  ```text
    97  go run server.go
    98  ```
    99  
   100  Browse to `http://localhost:3000` and you should see `Hello, World!` on the page.
   101  
   102  ### Basic routing
   103  
   104  Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (`GET`, `PUT`, `POST`, etc.).
   105  
   106  Each route can have **multiple handler functions** that are executed when the route is matched.
   107  
   108  Route definition takes the following structures:
   109  
   110  ```go
   111  // Function signature
   112  app.Method(path string, ...func(*fiber.Ctx) error)
   113  ```
   114  
   115  - `app` is an instance of **Fiber**
   116  - `Method` is an [HTTP request method](https://docs.gofiber.io/api/app#route-handlers): `GET`, `PUT`, `POST`, etc.
   117  - `path` is a virtual path on the server
   118  - `func(*fiber.Ctx) error` is a callback function containing the [Context](https://docs.gofiber.io/api/ctx) executed when the route is matched
   119  
   120  **Simple route**
   121  
   122  ```go
   123  // Respond with "Hello, World!" on root path, "/"
   124  app.Get("/", func(c *fiber.Ctx) error {
   125  	return c.SendString("Hello, World!")
   126  })
   127  ```
   128  
   129  **Parameters**
   130  
   131  ```go
   132  // GET http://localhost:8080/hello%20world
   133  
   134  app.Get("/:value", func(c *fiber.Ctx) error {
   135  	return c.SendString("value: " + c.Params("value"))
   136  	// => Get request with value: hello world
   137  })
   138  ```
   139  
   140  **Optional parameter**
   141  
   142  ```go
   143  // GET http://localhost:3000/john
   144  
   145  app.Get("/:name?", func(c *fiber.Ctx) error {
   146  	if c.Params("name") != "" {
   147  		return c.SendString("Hello " + c.Params("name"))
   148  		// => Hello john
   149  	}
   150  	return c.SendString("Where is john?")
   151  })
   152  ```
   153  
   154  **Wildcards**
   155  
   156  ```go
   157  // GET http://localhost:3000/api/user/john
   158  
   159  app.Get("/api/*", func(c *fiber.Ctx) error {
   160  	return c.SendString("API path: " + c.Params("*"))
   161  	// => API path: user/john
   162  })
   163  ```
   164  
   165  ### Static files
   166  
   167  To serve static files such as **images**, **CSS**, and **JavaScript** files, replace your function handler with a file or directory string.
   168  
   169  Function signature:
   170  
   171  ```go
   172  app.Static(prefix, root string, config ...Static)
   173  ```
   174  
   175  Use the following code to serve files in a directory named `./public`:
   176  
   177  ```go
   178  app := fiber.New()
   179  
   180  app.Static("/", "./public") 
   181  
   182  app.Listen(":3000")
   183  ```
   184  
   185  Now, you can load the files that are in the `./public` directory:
   186  
   187  ```bash
   188  http://localhost:8080/hello.html
   189  http://localhost:8080/js/jquery.js
   190  http://localhost:8080/css/style.css
   191  ```
   192  
   193  ### Note
   194  
   195  For more information on how to build APIs in Go with Fiber, please check out this excellent article
   196  [on building an express-style API in Go with Fiber](https://blog.logrocket.com/express-style-api-go-fiber/).