Go Programming/Simple Web App

There are multiple packages in the standard library. One of those packages is called net/http. The package can be used to make simple web applications. Here is a simple example that will output Hello world! when localhost:8080 is visited.

You might see some new things that appeared.

As you see, the fmt package has been imported again. However, the net/http package has also been imported. A function called helloFunc is declared. The http.ResponseWriter type is used in the function to construct an HTTP response. The *http.Request is a pointer to a HTTP request received by a server or something be sent by a client. In this line, the string "Hello world!" is being written to the ResponseWriter in order for it to be displayed in the output. The main function has been declared with two actions. The handler function, helloFunc has been connected to the "/" route. Finally, the ListenAndServe has been called with a port that is "8080" and no router has been given to the second parameter.

Now, when the program is ran, Hello world! is expected to be shown on the website: localhost:8080.