Skip to content

Instantly share code, notes, and snippets.

@sayden
Last active September 23, 2015 07:42
Show Gist options
  • Save sayden/fa1f53b17ca64cfa4b7b to your computer and use it in GitHub Desktop.
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
//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