Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e266a7d

Browse files
add: http request basic_template
1 parent ddb400f commit e266a7d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

‎backend/http/main.go‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
func apiControllerTest(w http.ResponseWriter, r *http.Request) {
10+
fmt.Fprintf(w, "Server is working and you've requested %s\n", r.URL.Path)
11+
12+
}
13+
14+
func getNoOfUser(w http.ResponseWriter, r *http.Request) {
15+
data := map[string]string{
16+
"name": "gaurav",
17+
"age": "20",
18+
}
19+
// Also, data is a map, so you can’t directly print it without formatting.
20+
fmt.Printf("%s", r.URL.Host)
21+
// fmt.Fprintf(w, "%v", data)
22+
23+
/* to set content-type header */
24+
w.Header().Set("content/type", "application/json")
25+
26+
/* encoding the data into json*/
27+
json.NewEncoder(w).Encode(data)
28+
}
29+
30+
func main() {
31+
32+
fmt.Println("/////////////Starting your server wait for a while\\\\\\\\\\\\\\")
33+
/* function to handle new request */
34+
http.HandleFunc("/api", apiControllerTest)
35+
http.HandleFunc("/api/user/count", getNoOfUser)
36+
37+
// running the server at certain port address
38+
http.ListenAndServe(":8080", nil)
39+
40+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /