Skip to content

gowthae/Go-Minimal-HTTP-Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

1️⃣ package main

package main

💡 Meaning:

This tells Go:

“This is the main program. Start running from here.”

Just like:

The engine of a car

The main door of a house

Every Go program must have this.

2️⃣ import (...)

import ( "fmt" "net/http" )

💡 Meaning: We are borrowing tools made by Go.

Tool Why we need it fmt To print messages net/http To build a web server

Like:

Borrowing a stove to cook

Borrowing a phone to talk

3️⃣ This is your web page function

func homePage(w http.ResponseWriter, r *http.Request) {

💡 Meaning: This line says:

“When anyone visits my website, run this function.”

Part Meaning func Making a function (a helper machine) homePage Name of the function w Used to send a reply to the browser r Used to read what the user asked

4️⃣ Sending message to the browser

fmt.Fprintln(w, "Hello! This is my first Go server!")

💡 Meaning: This line says:

“Send this text to the visitor’s browser.”

So when user opens:

http://localhost:8080

They will see:

Hello! This is my first Go server!

5️⃣ Closing the function

}

💡 Meaning: The work of this function is completed.

6️⃣ Start of main function

func main() {

💡 Meaning: This is the starting point of your program. Go always starts running from main().

7️⃣ Connecting webpage to a link

http.HandleFunc("/", homePage)

💡 Meaning: This tells Go:

“When someone visits / (home page), run homePage function.”

Symbol Meaning / Home page (like google.com/) homePage The function to run

8️⃣ Starting the server

http.ListenAndServe(":8080", nil)

💡 Meaning: This tells Go:

“Start a web server on port 8080 and keep it running.”

So now your website is available at:

http://localhost:8080

Your computer becomes a mini website hosting machine ✅

9️⃣ End of program

}


🧪 How to Run This on Your Laptop

Step 1 — Install Go

Download from:

https://go.dev

✅ Step 2 — Create a file

Create a file:

main.go

Paste the code inside it.

✅ Step 3 — Run in terminal go run main.go

✅ Step 4 — Open browser

Visit:

http://localhost:8080

You will see:

Hello from Go server!

🎉 YOU JUST CREATED A WEB SERVER 🎉

About

Built minimal version of HTTP server in GO for Backend fundamentals

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages