Skip to content

Instantly share code, notes, and snippets.

@codenoid
Last active April 27, 2020 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codenoid/6ea84492d24b3c76837f2e586366adcf to your computer and use it in GitHub Desktop.
Save codenoid/6ea84492d24b3c76837f2e586366adcf to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"runtime"
"time"
)
func main() {
http.HandleFunc("/", HelloServer)
go http.ListenAndServe(":8080", nil)
for {
fmt.Printf("Total Goroutine yang lagi di pake : %v\n", runtime.NumGoroutine())
time.Sleep(200 * time.Millisecond)
}
}
// route dipanggil 1
// route diproses 1
func HelloServer(w http.ResponseWriter, r *http.Request) {
fmt.Println("route dipanggil")
go func() {
// proses yang jalan selama 5 detik
fmt.Println("goroutine didalem route dibuat")
time.Sleep(7 * time.Second)
}()
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
// sync process yang jalan selama 3 detik
time.Sleep(3 * time.Second)
fmt.Println("route done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment