In this article we will learn about some of the frequently asked GO programming questions in technical like “Golang HTTP Server” Code Answer’s. When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks. Error or stack handling on go was simple and easy. An error message with filename, line number and a message describing the error is sent to the browser. This tutorial contains some of the most common error checking methods in GO. Below are some solution about “Golang HTTP Server” Code Answer’s.
Golang HTTP Server
xxxxxxxxxx
1
// Code by Divyanshu Shekhar - https://divyanshushekhar.com/golang-http-server/
2
3
// Golang HTTP Server
4
package main
5
6
import (
7
"fmt"
8
"log"
9
"net/http"
10
)
11
12
const (
13
// Host name of the HTTP Server
14
Host = "localhost"
15
// Port of the HTTP Server
16
Port = "8080"
17
)
18
19
func home(w http.ResponseWriter, r *http.Request) {
20
fmt.Fprintf(w, "This is a Simple HTTP Web Server!")
21
}
22
23
func main() {
24
http.HandleFunc("/", home)
25
err := http.ListenAndServe(Host+":"+Port, nil)
26
if err != nil {
27
log.Fatal("Error Starting the HTTP Server : ", err)
28
return
29
}
30
31
}
http go
xxxxxxxxxx
1
package main
2
3
import (
4
"fmt"
5
"net/http"
6
"time"
7
)
8
9
func greet(w http.ResponseWriter, r *http.Request) {
10
fmt.Fprintf(w, "Hello World! %s", time.Now())
11
}
12
13
func main() {
14
http.HandleFunc("/", greet)
15
http.ListenAndServe(":8080", nil)
16
}