Twix Router Introduction
Let's discover Twix Router for building HTTP services in Go.
Getting Started
Get started by creating a new project.
Or try the Twix Router with the basic usage example below.
What you'll need
- Go version 1.16 or above
Installation
Install the Twix Router package using go get:
go get github.com/farhanmobashir/twix
Usage
Here's a basic example of how to use the twix router:
package main
import (
"net/http"
"github.com/farhanmobashir/twix"
)
func main() {
router := twix.New()
router.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
})
http.ListenAndServe(":8080", router)
}
Steps
-
Create a new Go project:
mkdir my-twix-app
cd my-twix-app
go mod init my-twix-app -
Install Twix Router:
go get github.com/farhanmobashir/twix -
Create a main.go file:
package main
import (
"net/http"
"github.com/farhanmobashir/twix"
)
func main() {
router := twix.New()
router.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
})
http.ListenAndServe(":8080", router)
} -
Run your application:
go run main.go
Open your browser and navigate to http://localhost:8080/hello to see your Hello, World! message.
Feel free to edit main.go and add more routes as needed. The server reloads automatically when you make changes and restart it.
That's it! You now have a basic HTTP server running with Twix Router.