-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from antoniq/golang
Added example with go.
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
|