linux golang 代码检测本地端口是否被占用

可执行代码:

package main

import(
    "os/exec"
    "fmt"
)

func main(){
    fmt.Println(PortInUse(9001))
}

func PortInUse(port int) bool {
    checkStatement := fmt.Sprintf("lsof -i:%d ", port)
    output, _ := exec.Command("sh", "-c", checkStatement).CombinedOutput()
    if len(output) > 0 {
        return true
    }
    return false
}

 

posted @ 2019-11-08 15:44  小强CanFly  阅读(3334)  评论(0)    收藏  举报