İleri
Ders 21 / 22
HTTP ve Web
Go'nun net/http paketi production kalitesinde sunucu yazmanı sağlar.
go
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func anaSayfa(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "GoDev Türkiye API — Murat Tarakçı")
}
func saglik(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{
"status": "ok",
"site": "godevturkiye.com",
})
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", anaSayfa)
mux.HandleFunc("/saglik", saglik)
fmt.Println("http://localhost:8080")
http.ListenAndServe(":8080", mux)
}Deneme Tarayıcıda
http://localhost:8080/saglik aç. Blog'da REST API yazısı var.Alıştırma
/merhaba?ad=Murat sorgusunu okuyup JSON dönen endpoint yaz.