Your question is a little misleading as it asks how to open a local page in a web browser, but you really want to know how to start the web server so that it can be opened in the browser.
( - ) http.FileServer(). . js Go - golang, > .
/tmp/data:
http.Handle("/", http.FileServer(http.Dir("/tmp/data")))
panic(http.ListenAndServe(":8080", nil))
( Go), net/http , :
func myHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello from Go")
}
func main() {
http.HandleFunc("/", myHandler)
panic(http.ListenAndServe(":8080", nil))
}
( ), Go . , , . - :
func open(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default:
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}
Gowut ( Go Web UI Toolkit, : ).
- :
open("http://localhost:8080/")
: http.ListenAndServe() ( ). , goroutine, :
go open("http://localhost:8080/")
panic(http.ListenAndServe(":8080", nil))
, -: : , ?