Skip to content

Instantly share code, notes, and snippets.

@campoy
Last active Apr 18, 2019
Embed
What would you like to do?
Electron-like html+golang app
<html>
<head>
<title>Hello, from Go</title>
</head>
<body>
<h1>Hello</h1>
<p>
This is Go code running, {{.}}
</p>
</body>
</html>
// Copyright 2016 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to writing, software distributed
// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"html/template"
"log"
"net/http"
"os"
"os/exec"
)
const (
listen = "localhost:2016"
chrome = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
)
func main() {
http.HandleFunc("/", handler)
errc := make(chan error)
go func() { errc <- http.ListenAndServe(listen, nil) }()
if err := exec.Command(chrome, "--app=http://"+listen).Run(); err != nil {
log.Fatal(err)
}
log.Fatal(<-errc)
}
var tmpl = template.Must(template.ParseFiles("home.tmpl"))
func handler(w http.ResponseWriter, r *http.Request) {
if err := tmpl.Execute(w, os.Getenv("USER")); err != nil {
log.Printf("could not render home: %v", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment