Table of contents
Print "Hello World"
We are going to see how to create a simple HTTP server that listens on port 6688. When a request is made to the /helloworld
path, the helloworld()
function is called. This function simply prints "Hello World" to the response writer.
Create a file main.go
package main
import (
"fmt"
"log"
"net/http"
)
func helloworld(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World")
}
func main() {
http.HandleFunc("/helloworld", helloworld)
log.Fatal(http.ListenAndServe(":6688", nil))
}
Here is a more detailed explanation of the code:
The
package main
statement tells the Go compiler that this code is in themain
package.The
import
statements import thefmt
,log
, andnet/http
packages.The
func helloworld(w http.ResponseWriter, r *http.Request)
function is a simple function that prints "Hello World" to the response writer.The
func main()
function is the entry point for the program. It calls thehttp.HandleFunc()
method to tell the HTTP server to handle requests for the/helloworld
path with thehelloworld()
function. It then calls thehttp.ListenAndServe()
method to start the HTTP server.
To run the code, you can save it in a file called main.go and then run the following command:
$ go run main.go
This will start the HTTP server and you can then access it by opening a web browser and navigating to http://localhost:6688/helloworld.
Return JSON
We have seen how to print "Hello world" Now we are going to see how to create a server that when a request is made to the /getbook
path, the GetBook()
function is called. This function returns a JSON representation of a book object.
Create main1.go
package main
import (
"encoding/json"
"log"
"net/http"
)
type Book struct {
Title string `json:"title"`
Author string `json:"author"`
Pages int `json:"pages"`
}
func GetBook(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
book := Book{
Title: "Golang Book",
Author: "Google",
Pages: 300,
}
json.NewEncoder(w).Encode(book)
}
func main() {
http.HandleFunc("/getbook", GetBook)
log.Fatal(http.ListenAndServe(":6688", nil))
}
Here is a more detailed explanation of the code:
The
package main
statement tells the Go compiler that this code is in themain
package.The
import
statements import theencoding/json
,log
, andnet/http
packages.The
type Book struct
statement defines a struct calledBook
. This struct has three fields:Title
,Author
, andPages
.The
func GetBook(w http.ResponseWriter, r *http.Request)
function is the function that is called when a request is made to the/getbook
path. This function sets theContent-Type
header toapplication/json
and then encodes aBook
object into JSON and writes it to the response writer.The
func main()
function is the entry point for the program. It calls thehttp.HandleFunc()
method to tell the HTTP server to handle requests for the/getbook
path with theGetBook()
function. It then calls thehttp.ListenAndServe()
method to start the HTTP server.
To run the code, you can save it in a file called main1.go and then run the following command:
$ go run main1.go
This will start the HTTP server and you can then access it by opening a web browser and navigating to http://localhost:6688/getbook
Thanks for reading
I appreciate you taking the time to read this. If you found it helpful, please subscribe to my newsletter and give it a like.