可执行代码:
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 }