Hi this is Akshay Rao. I wanted to learn Go language, so thought of building a simple project. In this project ,I have built a simple a HTTP server which gives a html Response in the web browser when the paths are changed.
Let's Start
- We will have to specify the package, I am main because the package "main" instructs the Go compiler to build the package as an executable application rather than a shared library. Our executable application will launch from the "main" function in the package.
- Create a file name it as .go extension like I have named as “project.go”.
- GoLang has packages for all the commands, so we will have to import packages .
- fmt- will help in printing out the results and getting inputs from users.
- - html - to print the html response on the web browser.
- - log- if there is any error while running the serverit will log the error in terminal.
- - net/http - to create a server.
- Create a function main.
- Call the HandlerFunc from the http api, which will call the function specified in it fro the specified path.
- At last we will run the server with the help of ListenAndServe at 8080 and set the error message as nil.
- Wrap the ListenAndServe in log.Fatal to the show the error if it occurs.
- Then run “go run /folder name/.go”
Top comments (0)