Browse Source

完事打开浏览器

liuyuqi-dellpc 5 years ago
parent
commit
cd61955735
3 changed files with 27 additions and 1 deletions
  1. 2 1
      .gitignore
  2. 3 0
      main.go
  3. 22 0
      util/web.go

+ 2 - 1
.gitignore

@@ -1,2 +1,3 @@
 fs-go
-!.gitignore
+!.gitignore
+/.idea

+ 3 - 0
main.go

@@ -3,8 +3,10 @@ package main
 import (
 	"flag"
 	"fmt"
+	"github.com/klipitkas/fs-go/util"
 	"log"
 	"net/http"
+	"strconv"
 )
 
 var port int
@@ -32,4 +34,5 @@ func main() {
 			break
 		}
 	}
+	_ = util.OpenUrl("http://localhost:" + strconv.Itoa(port))
 }

+ 22 - 0
util/web.go

@@ -0,0 +1,22 @@
+package util
+
+import (
+	"fmt"
+	"os/exec"
+	"runtime"
+)
+var commonds = map[string]string{
+	"windows": "cmd /c start",
+	"darwin":  "open",
+	"linux":   "xdg-open",
+}
+
+//OpenUrl 调用系统命令打开浏览器
+func OpenUrl(url string) error {
+	run, ok := commonds[runtime.GOOS]
+	if !ok {
+		return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS)
+	}
+	cmd := exec.Command(run, url)  //拼凑命令: cmd /c start https://baidu.com
+	return cmd.Start()
+}