diff --git a/go/README.md b/go/README.md new file mode 100644 index 0000000..9249cb3 --- /dev/null +++ b/go/README.md @@ -0,0 +1,24 @@ +# "Hello World" in Go + +## Requirements +You need to install [Go](https://golang.org/doc/install). +Recommended IDE: Visual Studio Code + +## Build + +To build type +``` +go build main.go +``` + +## Run + +To run type +``` +./main +``` + +## Stop + +To stop the server just press Ctrl-C + diff --git a/go/main.go b/go/main.go new file mode 100644 index 0000000..ec6b27d --- /dev/null +++ b/go/main.go @@ -0,0 +1,29 @@ +package main + +import( + "encoding/json" + "log" + "net/http" //http client + "github.com/gorilla/mux" //router lib +) + +var message string + +type Text struct{ + Message string `json:"message"` +} + +func CreateMessage(w http.ResponseWriter, r *http.Request){ + name := r.FormValue("name") + w.Header().Set("Content-Type", "application/json;charset=UTF-8") + + message := Text{Message: "Hello " + name + "!"} + json.NewEncoder(w).Encode(message) //parse the message to JSON format +} + +func main(){ + router := mux.NewRouter() + router.Path("/say-hello").Queries("name", "{name}").HandlerFunc(CreateMessage).Methods("POST") + log.Fatal(http.ListenAndServe(":8080", router)) +} +