Skip to content

Instantly share code, notes, and snippets.

@rolaveric
Last active September 28, 2017 15:39
Show Gist options
  • Save rolaveric/9388326 to your computer and use it in GitHub Desktop.
Save rolaveric/9388326 to your computer and use it in GitHub Desktop.
A simple example of a Go library, and how to expose it to the global scope when run through GopherJS.
package main
import (
"github.com/gopherjs/gopherjs/js"
"github.com/rolaveric/pet"
)
func main() {
js.Global.Set("pet", map[string]interface{}{
"New": pet.New,
})
}
package pet
import "github.com/gopherjs/gopherjs/js"
type Pet struct {
name string
}
func (p *Pet) Name() string {
return p.name
}
func (p *Pet) SetName(newName string) {
p.name = newName
}
func New(name string) *js.Object {
return js.MakeWrapper(&Pet{name})
}
@pbrown12303
Copy link

It would really be helpful to see an example of an html page calling the New(), Name(), and SetName() functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment