Skip to main content

🦆 Go Network Programming Guide

Table of Contents

🌐 Comprehensive Guide to Go’s net/http and Network Programming
#

🚀 Introduction
#

Go’s net/http package is one of its most powerful standard libraries — it enables you to build web servers, clients, REST APIs, and even microservices. This guide will walk you through the fundamentals of network programming in Go, including HTTP, TCP, JSON, and gRPC.


📡 Part 1: Understanding Network Programming Basics
#

🧠 What is Network Programming?
#

Network programming enables communication between processes or devices over a network using protocols such as TCP/IP, HTTP, WebSocket, or gRPC.

🔗 Common Protocols Overview
#

Protocol Description Go Package
TCP (Transmission Control Protocol) Connection-oriented reliable stream net
UDP (User Datagram Protocol) Connectionless, fast, unreliable net
HTTP (HyperText Transfer Protocol) Foundation of web communication net/http
gRPC (Google Remote Procedure Call) High-performance RPC over HTTP/2 google.golang.org/grpc

🌍 Part 2: HTTP with Go
#

🧩 Creating a Basic HTTP Server
#

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World! 🌎 You requested: %s", r.URL.Path)
}

func main() {
    http.HandleFunc("/", handler)
    fmt.Println("🚀 Server running at http://localhost:8080")
    http.ListenAndServe(":8080", nil)
}

🛠 Key Functions in net/http
#

Function Purpose
http.HandleFunc(pattern, handler) Registers a handler function for a pattern.
http.ListenAndServe(addr, handler) Starts an HTTP server on a given address.
http.ResponseWriter Used to send a response to the client.
*http.Request Represents the incoming HTTP request.

🧾 Serving Static HTML Pages
#

You can serve static HTML files using Go’s built-in file server.

http.Handle("/", http.FileServer(http.Dir("./static")))
http.ListenAndServe(":8080", nil)

🧠 Helpful Tips
#

  • Always log requests for debugging.
  • Use environment variables for ports and configuration.
  • Serve static files from a separate /static directory.

🔌 Part 3: JSON APIs in Go
#

Example: JSON REST API Endpoint
#

package main

import (
    "encoding/json"
    "net/http"
)

type Article struct {
    Title  string `json:"title"`
    Author string `json:"author"`
}

func getArticle(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(Article{Title: "Learning Go", Author: "Keith"})
}

func main() {
    http.HandleFunc("/api/article", getArticle)
    http.ListenAndServe(":8080", nil)
}

⚡ Part 4: Building a TCP Server
#

Example
#

package main

import (
    "fmt"
    "net"
)

func main() {
    ln, _ := net.Listen("tcp", ":9000")
    for {
        conn, _ := ln.Accept()
        go func(c net.Conn) {
            fmt.Fprintln(c, "Welcome to the TCP server!")
            c.Close()
        }(conn)
    }
}

🛰️ Part 5: gRPC in Go
#

gRPC uses Protocol Buffers and HTTP/2 for efficient communication.

Install gRPC Tools
#

go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

Example .proto File
#

syntax = "proto3";

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply);
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

💡 Helpful Things to Remember
#

  • defer resp.Body.Close() — always close response bodies!
  • ⚙️ Reuse connections with http.Client for performance.
  • 🧱 Use context for cancellation in HTTP requests.
  • 🔐 HTTPS is easy with http.ListenAndServeTLS().
  • 🔥 Use net/http/pprof for performance profiling.
  • 🧭 Middleware can simplify logging, auth, etc.
  • 📦 Combine net/http with html/template for dynamic pages.

🧱 Part 6: Combining html/template with net/http
#

package main

import (
    "html/template"
    "net/http"
)

type PageData struct {
    Title   string
    Message string
}

func handler(w http.ResponseWriter, r *http.Request) {
    tmpl := template.Must(template.ParseFiles("index.html"))
    data := PageData{Title: "Welcome!", Message: "Hello from Go Templates"}
    tmpl.Execute(w, data)
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

📘 Conclusion
#

The net/http package is the backbone of Go web development. It’s minimalistic, fast, and flexible — letting you build from simple servers to enterprise APIs and microservices.

When combined with html/template, encoding/json, and frameworks like grpc, it becomes a complete web stack for backend developers.

Related