package main
import (
"log""net/http""time"
)
// LoggingMiddleware logs each incoming request.funcLoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
// BEFORE handler
log.Printf("Incoming request → %s %s", r.Method, r.URL.Path)
// Call next handler
next.ServeHTTP(w, r)
// AFTER handler
log.Printf("Completed → %s %s (%v)", r.Method, r.URL.Path, time.Since(start))
})
}
4️⃣ Code: resolver.go (business logic layer)
package main
// Resolver simulates fetching data or doing business logic.type Resolver struct{}
func(r *Resolver) GetGreeting(name string) string {
if name == "" {
name = "Guest"
}
return"Hello, " + name + "! Welcome to Go Middleware Demo."
}
5️⃣ Code: handler.go (HTTP handler)
package main
import (
"encoding/json""net/http"
)
type Handler struct {
Resolver *Resolver
}
func(h *Handler) GreetingHandler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
message := h.Resolver.GetGreeting(name)
resp := map[string]string{
"message": message,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resp)
}
6️⃣ Code: main.go (server setup)
package main
import (
"log""net/http"
)
funcmain() {
resolver := &Resolver{}
handler := &Handler{Resolver: resolver}
mux := http.NewServeMux()
mux.Handle("/greet", LoggingMiddleware(http.HandlerFunc(handler.GreetingHandler)))
log.Println("Server started on port 8080")
err := http.ListenAndServe(":8080", mux)
if err != nil {
log.Fatal(err)
}
}
7️⃣ Test the Server Locally
Run the app:
go run .
Test using browser or curl:
http://localhost:8080/greet?name=Vinod
Output:
{"message":"Hello, Vinod! Welcome to Go Middleware Demo."}
Logs:
Incoming request → GET /greet
Completed → GET /greet (1.2ms)
8️⃣ Add Docker Support (Dockerfile)
Create Dockerfile:
FROM golang:1.22-alpine AS build
WORKDIR /app
COPY . .
RUN go mod init go-middleware-demo || true
RUN go mod tidy
RUN go build -o server .
FROM alpine:3.19
WORKDIR /app
COPY --from=build /app/server .
EXPOSE 8080
CMD ["./server"]
Go (Golang) Basics — Files, Commands, go.mod, go.sum, go tidy, vendor, download, Compilation & Testing
A complete beginner-friendly guide
Go is a fast, simple, and powerful language created by Google for building server-side, cloud-native, and distributed systems.
This guide gives a full beginner overview of:
✔ Go project structure
✔ Go files and packages
✔ How to run & compile Go programs
✔ How testing works
✔ What go.mod & go.sum are
✔ go mod commands: tidy, vendor, download
✔ Important Go CLI commands
✔ Offline builds
✔ Reproducible CI builds
✔ Enterprise environments with blocked internet
Command:
go mod vendor
To compile using vendor folder:
go build -mod=vendor
✔ go mod download
Downloads all dependencies into Go’s module cache:
$GOPATH/pkg/mod
Command:
go mod download
Use cases:
For CI/CD systems
For Docker builds
For warming cache
When preparing offline builds
You can download specific modules:
go mod download github.com/gin-gonic/gin
✔ go mod verify
Verifies checksums in go.sum.
go mod verify
Ensures no dependency code was tampered with.
go get (Add/Upgrade Dependencies)
Add new dependency:
go get github.com/pkg/errors
Upgrade dependency:
go get -u github.com/pkg/errors
Remove dependency (indirectly):
go mod tidy
Go Build Modes
Cross-compile for Linux:
GOOS=linux GOARCH=amd64 go build
Cross-compile for macOS ARM:
GOOS=darwin GOARCH=arm64 go build
Cross-compile for Windows:
GOOS=windows GOARCH=amd64 go build -o app.exe
Go supports cross-compilation without extra tools.
Summary Table
Feature / Command
Purpose
go.mod
Lists module name + dependencies
go.sum
Stores dependency checksums
go run
Build + run app
go build
Compile binary
go test
Run tests
go mod tidy
Add missing + remove unused deps
go mod vendor
Copy dependencies into vendor/
go mod download
Download all required deps
go fmt
Format code
go get
Add/upgrade dependency
Final Text Diagram: Go Project Workflow
Write Go code (.go files)
│
▼
Initialize module
go mod init
│
▼
Add imports → dependencies appear in go.mod
│
▼
Clean module
go mod tidy
│
▼
Download deps
go mod download
│
▼
(Optional) create vendor folder
go mod vendor
│
▼
Build or run
go build
go run
│
▼
Write tests → go test
This is why threads can share data (Heap)
but not each other’s local variables (Stack).
Final Simple Summary
✔ Stack
Stores names (variable identifiers)
Stores references (IDs pointing into heap)
✔ Heap
Stores objects
Each object has an internal ID and a link to its class in Method Area
✔ Method Area
Stores class definitions and static values
✔ Communication
Stack → Heap via reference ID Heap → Method Area via class pointer
Everything begins with references.
One-Line Explanation
The Stack stores references, the Heap stores objects, and the Method Area stores class definitions. JVM connects all three using internal identifiers and pointers.