Go HTTP Server 🚀

beginner
17 min

Go HTTP Server 🚀

Welcome to the exciting world of Go (Golang)! Today, we're going to dive into creating a simple yet powerful HTTP server using Go. By the end of this lesson, you'll have a solid understanding of how to build, run, and test a Go HTTP server. 🎯

What is Go HTTP Server?

Go HTTP Server is a fundamental part of Go programming. It allows you to create web applications and APIs easily. With its simplicity and performance, it's a great choice for building high-traffic web services. 💡

Getting Started 🌐

Before we begin, make sure you have Go installed on your machine. You can check the installation by typing go version in your terminal. If it's installed, you'll see the version number.

Creating a New Go Project 📝

To create a new Go project, navigate to your desired project directory and run:

bash
go mod init <your_project_name>

Replace <your_project_name> with the name you'd like for your project.

Writing the Go HTTP Server Code 📝

Now, let's write a simple Go HTTP server. Create a new file named main.go in your project directory and paste the following code:

go
package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Welcome to Go HTTP Server!") }) fmt.Println("Server started on port 8080...") err := http.ListenAndServe(":8080", nil) if err != nil { panic(err) } }

This code creates a simple web server that listens on port 8080 and responds with "Welcome to Go HTTP Server!" whenever a request is made to the root URL (/).

Running the Go HTTP Server 🔄

To run the server, navigate to your project directory in the terminal and type:

bash
go run main.go

You should see "Server started on port 8080..." in the terminal. Now, open your web browser and navigate to http://localhost:8080. You should see "Welcome to Go HTTP Server!" on the screen. ✅

Advanced Example: Handling GET Requests 📝

Let's make our server more interesting by handling GET requests. Modify the code in main.go as follows:

go
package main import ( "fmt" "net/http" "strings" ) func hello(w http.ResponseWriter, r *http.Request) { name := r.URL.Query().Get("name") if name == "" { name = "Stranger" } fmt.Fprintf(w, "Hello, %s! Welcome to Go HTTP Server.", name) } func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { hello(w, r) return } fmt.Fprintf(w, "Welcome to Go HTTP Server!") }) fmt.Println("Server started on port 8080...") err := http.ListenAndServe(":8080", nil) if err != nil { panic(err) } }

Now, if you navigate to http://localhost:8080/?name=Alice in your web browser, you'll see "Hello, Alice! Welcome to Go HTTP Server." ✅

Quiz 📝

Quick Quiz
Question 1 of 1

What is the purpose of the `http.HandleFunc()` function in the Go HTTP Server code?

That's it for today! We've covered the basics of creating and running a Go HTTP Server. Now, go ahead and experiment with your own HTTP servers! 💡

Stay tuned for more lessons on Go, where we'll dive deeper into handling different types of HTTP requests, using Go templates for web pages, and more! 🚀