Building a Web Server in Go – InApps is an article under the topic Software Development Many of you are most interested in today !! Today, let’s InApps.net learn Building a Web Server in Go – InApps in today’s post !

Read more about Building a Web Server in Go – InApps at Wikipedia



You can find content about Building a Web Server in Go – InApps from the Wikipedia website

Go (Golang.org) is the system programming language that provides standard HTTP protocol support in its standard library, which makes it easy for developers to build and get a web server running very quickly. Meanwhile, Go offers developers a lot of flexibility. In this post, we lay out several ways to build an HTTP web server in Go and then offer an analysis about how and why these different approaches all work perfectly in Go.

Before we get started, I assume you have basic knowledge about how to write Go code, you understand HTTP and know what a web server is. Then, we can begin with the famous “hello world” example in an HTTP web server version.

It’s better to see the effect and then explain the detail. Create a file named http1.go and copy and paste the following code into the file:

In the terminal, execute the command go run http1.go, then open the browser and visit http://localhost:8000. You will seeHello world! is showing on your screen.

How did that happen? Well, any runnable package must be named main in Go, and here we have the main function and a hello function.

Read More:   Invent for Developers – InApps 2022

In the main function, we called the function http.HandleFunc from the package net/http to register another function to be the handle function, which is the hello function in this case. This function accepts two arguments. The first one is a string type pattern, which is the route you want to match and it’s the root path in the example. The second argument is a function with the signature func(ResponseWriter, *Request)(https://gowalker.org/net/http#HandleFunc). As you can see, our hello function has exactly the same signature, therefore we can pass it as the argument. The next line we called the http.ListenAndServe(":8000", nil) function to listen on localhost with port 8000. Ignore the nil argument just for now.

In the hello function, we have two arguments. One with type http.ResponseWriter and its corresponding response stream, which is actually an interface type. The second is *http.Request and its corresponding HTTP request. We don’t have to use all the arguments. Just like in the hello function, if we want the response string Hello world! in the browser, then we only use http.ResponseWriter. io.WriteString is a helper function to let you write a string into a given writable stream. In Go, we call it the io.Writer interface. It’s not the point here, so knowing it works for us is enough.

This example is a little bit more complicated:

 

In the example above, we don’t use the nil in function http.ListenAndServe any more. Instead, replace it with a variable with type *ServeMux. As you may guess, this example does exactly the same thing as the previous example but we use the variable mux to register the handle function instead of directly registering from the net/http package. What’s going on underneath?  Well, the reason you can directly register the handle function in the package level is because net/http has a default *ServeMux inside the package, where now we defined our own.

Read More:   Vital to JBoss, FeedHenry Finds a Home in Red Hat’s Mobile Platform – InApps Technology 2022

To make the example even more complicated, see the code below:

To confirm your guess, this time we’re doing the same thing again, which is showing the Hello world! string on the screen. However, we not only define the *ServeMux, but also the variable server with type http.Server. At this point, you should know why we are able to run and serve the HTTP server directly from the net/http package. Yes, it has a default server inside the package as well. A new thing we see here is the type myHandler we defined and its method ServeHTTP. How is it possible to define a custom type and use it in an unmodified function from a standard library in a static programming language? The truth is simple. The Handler is an interface and only required to implement one method whose signature is func(w http.ResponseWriter, r *http.Request)(https://gowalker.org/net/http#Handler) and must be named as ServeHTTP. The type myHandler has the method that the interface expects, so we are good.

Read More:   Commoditizing Kubernetes with the Cluster API – InApps Technology 2022

You can see how our code changed but for the same purpose. Don’t forget to try the three examples yourself, then make some changes and see how the change behaves. Hope you enjoy!

Jiahua Chen is a Gopher, web application developer, coding addict, video course maker, hardworking guy, and creator of Go Walker, Gopm, Gogs and Macaron. He’s also a verified teacher at 51CTO College, NetEase Cloud Class and UCAI.

Image via Flickr Creative Commons



Source: InApps.net

Rate this post
As a Senior Tech Enthusiast, I bring a decade of experience to the realm of tech writing, blending deep industry knowledge with a passion for storytelling. With expertise in software development to emerging tech trends like AI and IoT—my articles not only inform but also inspire. My journey in tech writing has been marked by a commitment to accuracy, clarity, and engaging storytelling, making me a trusted voice in the tech community.

Let’s create the next big thing together!

Coming together is a beginning. Keeping together is progress. Working together is success.

Let’s talk

Get a custom Proposal

Please fill in your information and your need to get a suitable solution.

    You need to enter your email to download

      Success. Downloading...