-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_websvr.go
43 lines (37 loc) · 833 Bytes
/
go_websvr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* Module: go_websvr.go
* Purpose: go simple web server
* Date: ? (examples), N/A WWH
* Notes:
* 1) To build:
* go build go_websvr.go
* 2) Ref: GO tour
*
*/
package main
import (
"fmt"
"log"
"net/http"
)
// define a struc and a Hello method on the struct
type Hello struct{}
func (h Hello) ServeHTTP(
w http.ResponseWriter,
r *http.Request) {
fmt.Fprint(w, "<HTML><H1>Hello</H1><BODY>Test 1, 2, 3!</BODY></HTML>")
}
// main function, define host and port
func main() {
port := 8082
host := "localhost"
hostinfo := fmt.Sprintf("%s:%d", host, port)
fmt.Printf("go_websvr listening on http://%s\n", hostinfo)
// define struct and serve with callback to that struct's
// ServeHTTP method defined above
var h Hello
err := http.ListenAndServe(hostinfo, h)
if err != nil {
log.Fatal(err)
}
}