Last active
September 28, 2017 15:39
-
-
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.
This file contains hidden or 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 ( | |
| "github.com/gopherjs/gopherjs/js" | |
| "github.com/rolaveric/pet" | |
| ) | |
| func main() { | |
| js.Global.Set("pet", map[string]interface{}{ | |
| "New": pet.New, | |
| }) | |
| } |
This file contains hidden or 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 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}) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would really be helpful to see an example of an html page calling the New(), Name(), and SetName() functions.