Last active
September 23, 2015 07:42
-
-
Save sayden/fa1f53b17ca64cfa4b7b to your computer and use it in GitHub Desktop.
A simple hello world web server that accepts one param. Written in Go
This file contains 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
//hello.go | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
) | |
func handler(writer http.ResponseWriter, request *http.Request) { | |
fmt.Fprintf(writer, "Welcome, %s!", request.URL.Path[1:]) | |
} | |
type Message struct { | |
Message string | |
Code int | |
} | |
func about (writer http.ResponseWriter, request *http.Request) { | |
writer.Header().Set("Content-Type", "application/json; charset=utf-8") | |
m := Message{Message:"Welcome to the SandovalEffect API, build v0.0.001.992, 6/22/2015 0340 UTC.", | |
Code:100} | |
b, err := json.Marshal(m) | |
if err != nil { | |
panic(err) | |
} | |
writer.Write(b) | |
} | |
func main() { | |
port := "8080" | |
fmt.Printf("Web server running in port %s", port) | |
http.HandleFunc("/", handler) | |
http.HandleFunc("/about/", about) | |
http.ListenAndServe(":"+port, nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment