2011|08|
2013|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|05|06|07|08|09|10|11|12|
2016|01|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|

2020-01-01 GOによる簡単なWebサーバの作り方 [長年日記]

http://www.kobore.net/soft/wstest4/go_server.mm.html

GOによる簡単なWebサーバの作り方

GOによる簡単なWebサーバの作り方

  • 初版 2020/01/01

    /home/kobore/www/soft/wstest4に一式あり

    1.目的

    • WebSocketと連動させる方法を考えたが、難しいので単体で作ることにした

    2.サーバプログラム

    • "./main.go"
      • package main
      • import (
        • "log"
        • "net/http"
      • )
      • func main() {
        • http.Handle("/", http.FileServer(http.Dir("static")))
        • if err := http.ListenAndServe(":8686", nil); err != nil {
          • log.Fatal("ListenAndServe: ", err)
        • }
      • }
    • 起動方法
      • >go run main.go

    3.クライアントプログラム

    • "./index.html"
      • コード
        • <!DOCTYPE html>
          • <head>
            • <title>HTMLの練習1</title>
          • </head>
          • <body>
            • 初めてのWebページ1です。
          • </body>
        • </html>
      • 起動方法
        • http://192.168.0.88:8686/
    • "./user.html"
      • コード
        • <!DOCTYPE html>
          • <head>
            • <title>HTMLの練習2</title>
          • </head>
          • <body>
            • 初めてのWebページ2です。
          • </body>
        • </html>
      • 起動方法
        • http://192.168.0.88:8686/user.html

    4.その他

    • 画像を表示したい場合等
    • "./static/image1.jpg
      • 起動方法
        • http://192.168.0.88:8686/image1.jpg
    • "./static/image2.jpg
      • 起動方法
        • http://192.168.0.88:8686/image2.jpg