Go Foundations·Practice drills 6

Drill 13: Real project — A standard-library HTTP service

Connect this example to a real project you could build.

Beginner 5 min read Lesson 140 of 300 Challenge included
What you will be able to do
  • Connect this example to a real project you could build.

Practice task

Connect this example to a real project you could build.

main.goGo
package main

import (
    "encoding/json"
    "net/http"
    "time"
)

func health(w http.ResponseWriter, _ *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(map[string]bool{"ok": true})
}

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("GET /api/health", health)

    server := &http.Server{
        Addr:              ":3000",
        Handler:           mux,
        ReadHeaderTimeout: 5 * time.Second,
    }
    server.ListenAndServe()
}
Your turn

Write your answer

Easy

Connect this example to a real project you could build.

TEXT · starter
Your answer…
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 140 of 300 · 0% of course