http go
package main
import (
"fmt"
"net/http"
"time"
)
func greet(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World! %s", time.Now())
}
func main() {
http.HandleFunc("/", greet)
http.ListenAndServe(":8080", nil)
}
http go
package main
import (
"fmt"
"net/http"
"time"
)
func greet(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World! %s", time.Now())
}
func main() {
http.HandleFunc("/", greet)
http.ListenAndServe(":8080", nil)
}
golang http example
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type Response struct {
Message string `json:"message"`
}
func GetRequest(rw http.ResponseWriter, req *http.Request) {
rw.Header().Add("Content-Type", "application/json")
if req.Method == "GET" {
data := Response{Message: "Hello World From - GET"}
json, _ := json.Marshal(data)
fmt.Fprint(rw, string(json))
} else {
data := Response{Message: "Bad Request"}
json, _ := json.Marshal(data)
fmt.Fprint(rw, string(json))
}
}
func PostRequest(rw http.ResponseWriter, req *http.Request) {
rw.Header().Add("Content-Type", "application/json")
if req.Method == "POST" {
data := Response{Message: "Hello World From - POST"}
json, _ := json.Marshal(data)
fmt.Fprint(rw, string(json))
} else {
data := Response{Message: "Bad Request"}
json, _ := json.Marshal(data)
fmt.Fprint(rw, string(json))
}
}
func main() {
http.HandleFunc("/get", GetRequest)
http.HandleFunc("/post", PostRequest)
log.Fatal(http.ListenAndServe(":8000", nil))
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us