FAQ
Reviewers: golang-dev_googlegroups.com,

Message:
Hello golang-dev@googlegroups.com (cc: golang-dev@googlegroups.com),

I'd like you to review this change to
https://code.google.com/p/go-tour/


Description:
gotour: automatically open browser window if possible
Fixes issue 51.

Please review this at https://codereview.appspot.com/6945074/

Affected files:
M gotour/local.go


Index: gotour/local.go
===================================================================
--- a/gotour/local.go
+++ b/gotour/local.go
@@ -20,6 +20,7 @@
"strconv"
"strings"
"sync"
+ "time"

// Imports so that go build/install automatically installs them.
_ "code.google.com/p/go-tour/pic"
@@ -73,7 +74,7 @@
log.Print(localhostWarning)
}

- log.Printf("Open your web browser and visit http://%s/", *httpListen)
+ go startBrowser("http://" + *httpListen)
log.Fatal(http.ListenAndServe(*httpListen, nil))
}

@@ -190,3 +191,35 @@
running.Unlock()
return buf.Bytes(), err
}
+
+// startBrowser tries to open the URL in a browser after waiting for
+// the http server starts
+func startBrowser(url string) {
+ // wait for the server start
+ for {
+ resp, err := http.Get(url)
+ if err == nil {
+ resp.Body.Close()
+ break
+ }
+ time.Sleep(10 * time.Millisecond)
+ }
+ // try to start the browser
+ prog := ""
+ args := []string{url}
+ switch runtime.GOOS {
+ case "darwin":
+ prog = "open"
+ case "windows":
+ prog = "cmd"
+ args = []string{"/c", "start", url}
+ default:
+ prog = "xdg-open"
+ }
+ cmd := exec.Command(prog, args...)
+ if err := cmd.Start(); err != nil {
+ log.Printf("Please open your web browser and visit %s", url)
+ } else {
+ log.Printf("A browser window should open, if not, please visit %s
manually", url)
+ }
+}

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedDec 18, '12 at 2:36p
activeDec 20, '12 at 6:52p
posts13
users2
websitegolang.org

2 users in discussion

Minux Ma: 8 posts Adg: 5 posts

People

Translate

site design / logo © 2022 Grokbase