Skip to content

Instantly share code, notes, and snippets.

@niratama
Created August 24, 2014 06:39
Show Gist options
  • Save niratama/6b0117c6c6f2d21b5687 to your computer and use it in GitHub Desktop.
Save niratama/6b0117c6c6f2d21b5687 to your computer and use it in GitHub Desktop.
goでhttpサーバ起動と同時にブラウザを開く例
package main
import (
"fmt"
"log"
"net/http"
"github.com/skratchdot/open-golang/open"
)
const html = `<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>Hello!</h1>
</body>
</html>
`
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, html)
}
func main() {
http.HandleFunc("/", helloHandler)
listen := make(chan bool)
go func() {
<-listen
open.Run("http://localhost:3000/")
log.Println("browser start")
}()
listen <- true
log.Fatal(http.ListenAndServe(":3000", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment