Skip to content

Instantly share code, notes, and snippets.

@ababup1192
Last active May 6, 2020 02:42
Show Gist options
  • Save ababup1192/80c1be4d6bca880ab5f2dd5c085fb748 to your computer and use it in GitHub Desktop.
Save ababup1192/80c1be4d6bca880ab5f2dd5c085fb748 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"strconv"
"strings"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
pathArray := strings.Split(r.URL.Path, "/")[1:]
pathHead := pathArray[0]
if pathHead == "echo" {
var echoString string
if len(pathArray) <= 1 {
echoString = ""
} else {
echoString = pathArray[1]
}
fmt.Fprint(w, echoString)
} else if pathHead == "add" && len(pathArray) >= 3 {
l, lErr := strconv.ParseInt(pathArray[1], 10, 64)
r, rErr := strconv.ParseInt(pathArray[2], 10, 64)
if lErr != nil || rErr != nil {
w.WriteHeader(400)
fmt.Fprint(w, "Parameter is not a integer.")
} else {
fmt.Fprint(w, l+r)
}
} else {
w.WriteHeader(404)
fmt.Fprint(w, "Not Found")
}
})
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment