Service Discovery は、このような問題を解決するように設計されています。簡単な解決策は、PID ごとに tmp ファイルを作成し、各ポートを対応するファイルに書き込むことです。必要に応じてポートを読み取りますgo tool pprof
。
http.ListenAndServe("localhost:" + PORT, nil)
tmpFilePath := filePath.Join(os.TempDir(), "myport_" + strconv.FormatInt(os.Getpid(), 10))
f, _ := os.OpenFile(tmpFilePath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
f.Write(byte[](strconv.FormatInt(PORT, 10)))
f.Close()
go tool prof bash: go tool http://localhost:`cat /tmp/myport_10303`/debug/pprof/profile
実際にはテストされていません。構文エラーの可能性があります
アップデート:
go ソースを変更しないもう 1 つの方法は、netstat/lsof などの bash コマンドを使用して、go プロセスのリッスン ポートを見つけることです。お気に入り:
netstat -antp | grep 10303| grep LISTEN | awk '{ print $4 }' | awk -F: '{print $2}'
参考までに、最高のbashスクリプトではないと思います。