LoginSignup
0
1

More than 5 years have passed since last update.

http serverを立てたは良いが、Runningし続けるのを止められない

Last updated at Posted at 2018-06-16

このエントリの目的

  • Golangで立てたHttp serverをどうにかしてストップさせる

環境

go1.10.3 windows/amd64
Windows10
VSCode 最新版(2018/06/16時点)

http serverを立てた

main.go
package main

import (
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("http.ResponseWriter sample"))
}

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

localhost:8080にアクセスすると、http.ResponseWriter sampleが表示される。
というものです。

Code is already running!

VSCodeのpowershellを使ってgo runなどをしていたのですが、
先程のmain.goを実行したあとに、今度は別のまた学習用のコードを実行しようとしたらCode is already running!と怒られました。

統合ターミナルの「出力」を見ると、ずっとこの表示のまま。Doneがおこなわれず。
[Running] go run "c:\Users\user\go\src\debug_sample\main.go"

お前を...殺す

サーバを立ち上げたのと、別ファイルが実行できないことの理由の紐づけができず、結局http serverをkillすることになりました。

▽参考
golang で HTTP Server を停止する方法

PowerShellからprocessを探したり止めたり

現在立ち上がってるプロセス一覧を見る
PS C:\Users\user\go> ps

プロセスIDを使って殺す(http serverを)
PS C:\Users\user\go> Stop-Process -ID 0000

本当に死んだか?(http serverが)
PS C:\Users\user\go> Get-Process -Name httpserver

ひとまず、http serverを止めることができた。

勉強すべきことが出来た

  • コード上で立ち上げたサーバを、コード上で止める方法
  • なぜ、http.ListenAndServeは他の処理を邪魔するのか
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1