Skip to content

Commit 9fd8a3b

Browse files
committed
adding go-hello for ch5 lab
1 parent 05a50b0 commit 9fd8a3b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

go-hello/app.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"flag"
6+
"fmt"
7+
)
8+
9+
var lang = flag.String("lang", "en", "run app with language support - default is english")
10+
11+
func main() {
12+
var port = "8080"
13+
http.HandleFunc("/", rootHandler)
14+
fmt.Printf("Starting server on port %v...\n", port)
15+
http.ListenAndServe(":"+port, nil)
16+
}
17+
18+
func rootHandler(response http.ResponseWriter, request *http.Request) {
19+
20+
flag.Parse()
21+
22+
switch *lang {
23+
case "en":
24+
fmt.Fprintf(response, "Hello %s!. Welcome!\n", request.URL.Path[1:])
25+
case "es":
26+
fmt.Fprintf(response, "Hola %s!. Bienvenido!\n", request.URL.Path[1:])
27+
default:
28+
fmt.Fprintf(response, "Error! unknown lang option -> %s\n", *lang)
29+
}
30+
}

0 commit comments

Comments
 (0)