go言語インストール

https://go.dev/dl/

ファイルサーバ

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    fileServer := http.FileServer(http.Dir("."))
    http.Handle("/", fileServer) 

    fmt.Println("Server started on port 8080")
    log.Fatal(http.ListenAndServe(":8080", nil))
}

シンプルなカウントアップだけのエンドポイント

package main

import (
	"fmt"
	"net/http"
	"sync"
)

var counter int
var mutex sync.Mutex

func handler(w http.ResponseWriter, r *http.Request) {
	mutex.Lock()
	defer mutex.Unlock()

	counter++
	fmt.Fprintf(w, "リクエスト回数: %d\n", counter)
}

func main() {
	http.HandleFunc("/countup", handler) // パスを "/countup" に変更

	fmt.Println("サーバーを開始しました。http://localhost:8080/countup にアクセスしてください。")
	http.ListenAndServe(":8080", nil)
}
トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2024-07-06 (土) 20:39:36 (227d)