Being a Woman in Tech
3 min read
02nd Sep 2023
05th Mar 2024
2 mins read
You're wondering, we've written our first go program, but how can we see the outcome beyond the terminal or command line. I got you.
In our project file, let's add a html file and call it home.html which contains simple contents such as the title and paragraph in which we intend to serve.
Then in our main.go file
Comment out the line in which we printed Hello World.
We are going to add a function that's going to be called everytime a request is made to a certain url using the http package.The function called Handle Func which accepts two parameters, the first parameter is the url path, and the second is the function that is going to be invoked everytime a function comes in that matches the path func(w http.ResponseWriter, r *http.Request).
package main
import (
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./home.html")
})
http.ListenAndServe(":3000", nil)
}