If you already familiar with basic go syntax and wondering how can you write a web app in golang then you've come to the right place. In this post we will see how we can get started with writing our first hello world server in GoLang

Step 1: Listening to port

Let's say we want to start our app in http://localhost:8080 on our local machine. Let's listen to that port by:

http.ListenAndServe(":8080", nil)

Now if we run only this, it won't give us any response because we didn't define what need to be retuned in response when we hit this url.

Step 2: Define what need to be returned in response

HandleFunc( pattern string, handler func(ResponseWriter, *Request){} )

We can define our handler by using HandleFunc witch accept path and the handler

http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
    fmt.Fprintf(writer, "hello world at path %s", request.URL.Path)
})

That's it ! we are done with our hello world program in golang. What else we need to get started, huh!

Testing our first app

Visit this url: http://localhost:8080 and you should see the output. You can also try visiting different paths such as http://localhost:8080/my-profile to play around

Happy hello world!