# Start up

* Start up me-listen semua domain, port 9000 dan menghandle-nya dengan fungsi helloworld
* Untuk start up, kita menggunakan fungsi-fungsi pada [paket net/http](https://golang.org/pkg/net/http), yaitu http.HandlerFunc() dan http.ListenAndServe()

```go
package main

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

func main() {

    // handler
    handler := http.HandlerFunc(helloworld)

    // start server listening
    if err := http.ListenAndServe("0.0.0.0:9000", handler); err != nil {
        log.Fatalf("error: listening and serving: %s", err)
    }
}

// helloworld: basic http handler with response hello world string
func helloworld(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello World!")
}
```

* Kita juga bisa mendefinisikan parameter parameter untuk menjalankan server http melalui struct [http.Server](https://golang.org/pkg/net/http/#Server)

```go
package main

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

func main() {

    // parameter server
    server := http.Server{
        Addr:         "0.0.0.0:9000",
        Handler:      http.HandlerFunc(helloworld),
        ReadTimeout:  5 * time.Second,
        WriteTimeout: 5 * time.Second,
    }

    // mulai listening server
    if err := server.ListenAndServe(); err != nil {
        log.Fatalf("error: listening and serving: %s", err)
    }
}

// helloworld: basic http handler dengan response string hello world
func helloworld(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello World!")
}
```

* Listening server bisa dijalankan secara asynchronous melalui go routine. Dan untuk menangkap error yang terjadi digunakan channel.

```go
package main

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

func main() {

    // parameter server
    server := http.Server{
        Addr:         "0.0.0.0:9000",
        Handler:      http.HandlerFunc(helloworld),
        ReadTimeout:  5 * time.Second,
        WriteTimeout: 5 * time.Second,
    }

    serverErrors := make(chan error, 1)
    // mulai listening server
    go func() {
        log.Println("server listening on", server.Addr)
        serverErrors <- server.ListenAndServe()
    }()

    if err, ok := <-serverErrors; ok {
        log.Fatalf("error: listening and serving: %s", err)
    }
}

// helloworld: basic http handler dengan response string hello world
func helloworld(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello World!")
}
```


---

# 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://golang-microservices.rijalasepnugroho.com/build-rest-api-framework/start-up.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.
