Last active
May 6, 2020 02:42
-
-
Save ababup1192/80c1be4d6bca880ab5f2dd5c085fb748 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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