Skip to content

Commit

Permalink
Merge pull request #11 from antoniq/golang
Browse files Browse the repository at this point in the history
Added example with go.
  • Loading branch information
slu-it authored Sep 18, 2019
2 parents cd96f78 + d77bdc6 commit cf08328
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
24 changes: 24 additions & 0 deletions go/README.md
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

29 changes: 29 additions & 0 deletions go/main.go
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))
}

0 comments on commit cf08328

Please sign in to comment.