Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Web 開發與實戰應用

Go Web 開發、WebSocket、實時通訊與跨語言整合。

🌐 Web 開發基礎

Web 框架

📡 實時通訊

WebSocket

實時通訊平台

  • LiveKit - LiveKit 實時音視頻

🔗 跨語言整合

Python + Go

  • pytago - Python 與 Go 整合

🎯 完整指南

語言特性對比

💡 Web 開發實踐

HTTP Server

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, World!")
    })

    http.ListenAndServe(":8080", nil)
}

RESTful API

func handleUsers(w http.ResponseWriter, r *http.Request) {
    switch r.Method {
    case "GET":
        // 獲取用戶列表
    case "POST":
        // 創建用戶
    case "PUT":
        // 更新用戶
    case "DELETE":
        // 刪除用戶
    }
}

Middleware

func loggingMiddleware(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        log.Printf("%s %s", r.Method, r.URL.Path)
        next.ServeHTTP(w, r)
    })
}

🔌 WebSocket 實踐

基本連接

import "github.com/gorilla/websocket"

var upgrader = websocket.Upgrader{
    CheckOrigin: func(r *http.Request) bool {
        return true
    },
}

func wsHandler(w http.ResponseWriter, r *http.Request) {
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        return
    }
    defer conn.Close()

    for {
        _, message, err := conn.ReadMessage()
        if err != nil {
            break
        }
        conn.WriteMessage(websocket.TextMessage, message)
    }
}

🌉 Python + Go 整合

CGo 調用

// export Add
func Add(a, b int) int {
    return a + b
}

gRPC

// 定義服務
service Calculator {
    rpc Add(Numbers) returns (Result);
}

最後更新: 2025-12-01