forked from adnaan/learngo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleserver_0.go
43 lines (39 loc) · 1.36 KB
/
simpleserver_0.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
// Package main is the entry point of an executable Go program
// Are there non-executable Go programs? Well Yes, they are called...wait-for-it
// ...Packages
package main
// The import block. Each import is a package
// Notice how the methods we use from a package have their
//first letter capitalized.
import (
"fmt"
"io"
"log"
"net/http"
)
// Declaring it outside a function body hence
// the following is valid
var port = "3333"
// while this is not
// port:=3333
// Handling the request and replying back
// Umm Guy, you have your argument types reversed...
// Yeah this is how we roll. Is this easier to read?
func handle(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, fmt.Sprintf("Hello from port %q \n", port))
}
// Entry point of the package main
func main() {
//But hey we can do that weird (:=) declaration in here
// Also if it looks like a string, prints like a string, it's probably a string
duck := "QuackyQuack"
// Handle a route
http.HandleFunc("/", handle)
// Oh look format specifiers %q, %s, %d
log.Printf("%q on localhost:%q\n",duck,port)
// And serve it and pipe the error value directly into a log
log.Fatal(http.ListenAndServe(":"+port, nil))
// How do we test this? Let's bring out the curls and the postman's?
// Ah no, lets TDD this program.
// go run simpleserver_0.go and wait...Don't you use curl or anything else..
}