Skip to content

Commit ada13f1

Browse files
committed
add json sample
1 parent 9485d93 commit ada13f1

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

json/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# goji json sample
2+
3+
Run server:
4+
5+
```
6+
$ go run json.go
7+
```
8+
9+
Request to the server:
10+
11+
```
12+
curl http://localhost:8000/hello/gopher
13+
```

json/json.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/zenazn/goji"
8+
"github.com/zenazn/goji/web"
9+
)
10+
11+
type Hello struct {
12+
Name string
13+
Msg string
14+
}
15+
16+
func hello(c web.C, w http.ResponseWriter, r *http.Request) {
17+
name := c.URLParams["name"]
18+
if name == "" {
19+
name = "gopher"
20+
}
21+
22+
hello := &Hello{
23+
Name: name,
24+
Msg: "Hello",
25+
}
26+
27+
encoder := json.NewEncoder(w)
28+
encoder.Encode(hello)
29+
}
30+
31+
func main() {
32+
goji.Get("/hello/:name", hello)
33+
goji.Serve()
34+
}

0 commit comments

Comments
 (0)