Skip to content

Instantly share code, notes, and snippets.

@apstndb
Last active March 27, 2019 15:10
Show Gist options
  • Save apstndb/456e47ce0dcca191d967aefed1320b92 to your computer and use it in GitHub Desktop.
Save apstndb/456e47ce0dcca191d967aefed1320b92 to your computer and use it in GitHub Desktop.
cgo is enabled on App Engine Go 1.11

You can use cgo on App Engine Go 1.11.

deploy

$ gcloud app deploy --quiet

call

$ curl -s $(gcloud app browse --service=cgo111 --no-launch-browser)
42
runtime: go111
service: cgo111
module cgo111
package main
// typedef int (*intFunc) ();
//
// int
// bridge_int_func(intFunc f)
// {
// return f();
// }
//
// int fortytwo()
// {
// return 42;
// }
import "C"
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
f := C.intFunc(C.fortytwo)
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintln(int(C.bridge_int_func(f)))))
})
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("PORT")), nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment