# Root Routing

Pada sesi sebelumnya kita telah berhasil membuat 2 buah route yaitu `/` sebagai root dan `/author` . Namun apa yang terjadi jika kita mengakses route yang belum terdaftar?

Mari kita jalankan kembali kode yang ada pada sesi sebelumnya:

```go
package main

import (
	"fmt"
	"log"
	"net/http"
)

func handlerIndex(w http.ResponseWriter, r *http.Request) {
	data := "Welcome to Web Applications written by GO"
	w.Write([]byte(data))
}

func handlerAuthor(w http.ResponseWriter, r *http.Request) {
	data := "David Winalda"
	w.Write([]byte(data))
}

func main() {
	http.HandleFunc("/", handlerIndex)
	http.HandleFunc("/author", handlerAuthor)

	port := "localhost:3030"

	log.Println("Server started at", port)

	err := http.ListenAndServe(port, nil)

	if err != nil {
		fmt.Println(err.Error())
	}
}
```

Kita coba akses route yang belum terdaftar misalnya `/product` :

![Program tetap berjalan walaupun routing product belum terdaftar](/files/-MTBQDTn1irkNFnKHe_S)

Pada tampilan di atas terlihat bahwa walaupun routing `/product` belum terdaftar, kita tetap bisa menjalankan program. Namun akan mengakses root darti routing yang telah didaftarkan yaitu `/` . Untuk mengatasi hal ini, kita dapat menambahkan beberapa baris kode.

```go
package main

import (
	"fmt"
	"log"
	"net/http"
)

func handlerIndex(w http.ResponseWriter, r *http.Request) {
	// Membuat kondisi jika ada route yang belum terdaftar
	if r.URL.Path != "/" {
		http.NotFound(w, r)
		return
	}

	data := "Welcome to Web Applications written by GO"
	w.Write([]byte(data))
}

func handlerAuthor(w http.ResponseWriter, r *http.Request) {
	data := "David Winalda"
	w.Write([]byte(data))
}

func main() {
	http.HandleFunc("/", handlerIndex)
	http.HandleFunc("/author", handlerAuthor)

	port := "localhost:3030"

	log.Println("Server started at", port)

	err := http.ListenAndServe(port, nil)

	if err != nil {
		fmt.Println(err.Error())
	}
}
```

Kita menambahkan kondisional di dalam fungsi `handlerIndex` untuk mengatasi jika ada yang mengakses routing yang belum terdaftar:

```go
	if r.URL.Path != "/" {
		http.NotFound(w, r)
		return
	}
```

Jika dijalankan kembali route `/product` , maka program akan merespon seperti berikut ini:

![Route product tidak ditemukan](/files/-MTBRs94a0iGZte7Y1eu)


---

# 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://davidwinalda94.gitbook.io/mastering-golang/go-basic-for-web-development/root-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.
