Created
August 24, 2014 06:39
-
-
Save niratama/6b0117c6c6f2d21b5687 to your computer and use it in GitHub Desktop.
goでhttpサーバ起動と同時にブラウザを開く例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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