# Routing

Routing merupakan deklarasi path pada HTTP server. Untuk membuat HTTP routing di native golang diperlukan request Method. Request sendiri merupakan informasi yang dikirim oleh client. Request yang berguna untuk routing adalah request method yang digunakan untuk memperoleh HTTP method yang dikirim oleh client. Berikut adalah contoh *code* untuk routing di native golang.

```go
package main

import (
	"fmt"
	"net/http"
)

func main() {
	mux := http.NewServeMux()

	// request -> data type: struct
	var handlerMain http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
		if r.Method == http.MethodGet {
			fmt.Fprintf(w, r.Method)
		} else if r.Method == http.MethodPost {
			fmt.Fprintf(w, r.Method)
		} else if r.Method == http.MethodPatch {
			fmt.Fprintf(w, r.Method)
		} else if r.Method == http.MethodDelete {
			fmt.Fprintf(w, r.Method)
		}
	}
	mux.HandleFunc("/", handlerMain)

	fmt.Println("Server running")
	server := http.Server{
		Addr:   "localhost:5000",
		Handler: mux,
	}

	err := server.ListenAndServe()
	if err != nil{
		panic(err.Error())
	}
}
```

<figure><img src="/files/1M2z9ie88Rp9UgdMZFOy" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/JZcrQdytf9f4vvQmQirA" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/go-native-http/routing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
