Created
November 14, 2019 06:46
-
-
Save heiwa4126/908279117217a9e66f86b8a74401b986 to your computer and use it in GitHub Desktop.
golangのnet/httpで、違うポートで2つhttpを立ち上げるテスト。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 異なるポートで2つhttpdを立ち上げる。タイムアウトも設定してご安全に。 | |
package main | |
import ( | |
"fmt" | |
"net/http" | |
"time" | |
) | |
func handler1(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, "Hello World!\n") | |
} | |
func handler2(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, "Good-bye Cruel World...\n") | |
} | |
func newServer(addr string) (*http.ServeMux, *http.Server) { | |
mux := http.NewServeMux() | |
return mux, &http.Server{ | |
Addr: addr, | |
Handler: mux, | |
ReadHeaderTimeout: 5 * time.Second, | |
} | |
} | |
func main() { | |
mux1, srv1 := newServer(":8081") | |
mux2, srv2 := newServer(":8082") | |
go func() { | |
mux1.HandleFunc("/", handler1) | |
srv1.ListenAndServe() | |
}() | |
mux2.HandleFunc("/", handler2) | |
srv2.ListenAndServe() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment