go言語でファイルサーバ
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
* go言語インストール [#h4f69f72]
https://go.dev/dl/
* ファイルサーバ [#fb7e3e3f]
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))
}
* シンプルなカウントアップだけのエンドポイント [#efdfee99]
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) // パスを "/countu...
fmt.Println("サーバーを開始しました。http://localhost:8...
http.ListenAndServe(":8080", nil)
}
終了行:
* go言語インストール [#h4f69f72]
https://go.dev/dl/
* ファイルサーバ [#fb7e3e3f]
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))
}
* シンプルなカウントアップだけのエンドポイント [#efdfee99]
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) // パスを "/countu...
fmt.Println("サーバーを開始しました。http://localhost:8...
http.ListenAndServe(":8080", nil)
}
ページ名: