# Router

Untuk membuat router bisa menggunakan method sesuai HTTP Verb yang akan dipakai. Contoh untuk GET bisa menggunakan method GET seperti contoh code berikut.

## Contoh code router dengan HTTP method GET

```go
package main

import (
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	r.GET("/welcome", welcome)
	r.Run(":5000")
}

func welcome(ctx *gin.Context) {
	ctx.Writer.Write([]byte("success"))
}
```

<figure><img src="https://1578455751-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F09pucuEBc5BTaaur3uoo%2Fuploads%2F6zDvW6540bACgfdXz2lu%2F1.png?alt=media&#x26;token=ab967ad4-d5cf-4f1d-9f18-b1775cc6dd09" alt=""><figcaption></figcaption></figure>

## Contoh code router dengan HTTP method GET, POST, PATCH dan DELETE

```go
package main

import (
	"net/http"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/", GetHandler)
	r.POST("/", GetHandler)
	r.PATCH("/", GetHandler)
	r.DELETE("/", GetHandler)
	r.Run(":5000")
}

func GetHandler(ctx *gin.Context) {
	if ctx.Request.Method == http.MethodGet {
		ctx.Writer.Write([]byte("test get"))
	} else if ctx.Request.Method == http.MethodPost {
		ctx.Writer.Write([]byte("test post"))
	} else if ctx.Request.Method == http.MethodPatch {
		ctx.Writer.Write([]byte("test patch"))
	} else if ctx.Request.Method == http.MethodDelete {
		ctx.Writer.Write([]byte("test delete"))
	}
}
```
