{"id":1429,"date":"2021-04-16T15:40:22","date_gmt":"2021-04-16T06:40:22","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/?p=1429"},"modified":"2021-04-16T15:40:22","modified_gmt":"2021-04-16T06:40:22","slug":"go-to-js","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2021\/04\/16\/go-to-js\/","title":{"rendered":"GopherJS\u3092\u5229\u7528\u3057\u3066\u3001Go\u3092JavaScript\u306b\u5909\u63db\u3059\u308b"},"content":{"rendered":"

\u6982\u8981<\/h2>\n

\u4eca\u56de\u306f\u3001Go\u3092Web\u30d6\u30e9\u30a6\u30b6\u4e0a\u3067\u52d5\u4f5c\u3055\u305b\u308b\u305f\u3081\u306b\u3001GopherJS\u3092\u5229\u7528\u3059\u308b\u65b9\u6cd5\u3092\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n

\u4ed6\u306b\u3082\u65b9\u6cd5\u3084\u30c4\u30fc\u30eb\u306f\u3042\u308a\u307e\u3059\u304c\u3001\u79c1\u304c\u8aad\u3093\u3067\u3044\u305f\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u4f7f\u308f\u308c\u3066\u3044\u3066\u8208\u5473\u3092\u6301\u3063\u305f\u305f\u3081\u3001\u4eca\u56de\u306fGopherJS\u306b\u3064\u3044\u3066\u8abf\u67fb\u3057\u307e\u3057\u305f\u3002<\/p>\n

\u516c\u5f0f\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u8aad\u3093\u3067\u3082\u3001\u3072\u3068\u307e\u305a\u52d5\u4f5c\u78ba\u8a8d\u3059\u308b\u65b9\u6cd5\u304c\u5206\u304b\u308a\u306b\u304f\u304b\u3063\u305f\u305f\u3081\u3001\u672c\u8a18\u4e8b\u3067\u306f\u3001\u7c21\u5358\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u52d5\u4f5c\u3055\u305b\u308b\u3068\u3053\u308d\u307e\u3067\u3092\u89e3\u8aac\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

GopherJS\u3068\u306f<\/h2>\n

GopherJS
\nhttps:\/\/github.com\/gopherjs\/gopherjs<\/a><\/p>\n

GopherJS\u306f\u3001Go\u3092JavaScript\u306b\u5909\u63db\u3059\u308b\u30c4\u30fc\u30eb\u3067\u3059\u3002
\n\u3053\u306e\u3053\u3068\u306b\u3088\u3063\u3066\u3001Go\u30d7\u30ed\u30b0\u30e9\u30e0\u3092Web\u30d6\u30e9\u30a6\u30b6\u3067\u5b9f\u884c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002
\nCgo\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u306a\u3044\u3068\u3044\u3046\u5236\u7d04\u306f\u3042\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n

Go\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n

\u73fe\u72b6\u3001GopherJS\u306fGo 1.16\u4ee5\u964d\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u3067\u52d5\u4f5c\u3057\u307e\u3059\u3002
\n\u624b\u5143\u306ePC\u3067Go\u304c\u5165\u3063\u3066\u3044\u306a\u3044\u5834\u5408\u306f\u3001Homebrew\u306a\u3069\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n

\u3082\u3057\u3001\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u7570\u306a\u308bGo\u304c\u3059\u3067\u306b\u5165\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u3001
\n\u4ee5\u4e0b\u306e\u624b\u9806\u3067\u3001GopherJS\u7528\u306eGo\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u304d\u307e\u3059\u3002<\/p>\n

$ go get golang.org\/dl\/go1.16.3\n$ go1.16.3 download\n$ export GOPHERJS_GOROOT="$(go1.16.3 env GOROOT)"<\/code><\/pre>\n

GopherJS\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h2>\n

GopherJS\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u306f\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u884c\u306a\u3048\u307e\u3059\u3002<\/p>\n

$ go get -u github.com\/gopherjs\/gopherjs<\/code><\/pre>\n

gopherjs\u30b3\u30de\u30f3\u30c9\u304c\u5b9f\u884c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n

$ gopherjs\nGopherJS is a tool for compiling Go source code to JavaScript.\n\nUsage:\n  gopherjs [command]\n\nAvailable Commands:\n  build       compile packages and dependencies\n  doc         display documentation for the requested, package, method or symbol\n  get         download and install packages and dependencies\n  help        Help about any command\n  install     compile and install packages and dependencies\n  run         compile and run Go program\n  serve       compile on-the-fly and serve\n  test        test packages\n  version     print GopherJS compiler version\n\nFlags:\n  -h, --help   help for gopherjs\n\nUse "gopherjs [command] --help" for more information about a command.<\/code><\/pre>\n

\u5909\u63db\u5bfe\u8c61\u306eGo\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210<\/h2>\n

\u4ee5\u4e0b\u306e\u3088\u3046\u306bGo\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n

package main\n\nimport "github.com\/gopherjs\/gopherjs\/js"\n\nfunc main() {\n    js.Global.Set("pet", map[string]interface{}{\n        "New": New,\n    })\n    js.Global.Get("document").Call("write", "Hello world!")\n}\n\ntype Pet struct {\n    name string\n}\n\nfunc New(name string) *js.Object {\n    return js.MakeWrapper(&Pet{name})\n}\n\nfunc (p *Pet) Name() string {\n    return p.name\n}\n\nfunc (p *Pet) SetName(name string) {\n    p.name = name\n}<\/code><\/pre>\n

js.Global<\/code> \u304c\u8a18\u8ff0\u3055\u308c\u3066\u3044\u308b\u90e8\u5206\u304c\u3001Go\u304b\u3089\u306e\u3084\u308a\u3068\u308a\u306e\u90e8\u5206\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n

js.Global.Get("document").Call("write", "Hello world!")` <\/code><\/pre>\n

\u306e\u90e8\u5206\u306f\u3001Hello world!<\/code>\u3068\u3044\u3046\u6587\u5b57\u5217\u3092\u8868\u793a\u3055\u305b\u308b\u51e6\u7406\u3092\u8a18\u8ff0\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

js.Global.Set("pet", map[string]interface{}{\n        "New": New,\n    })<\/code><\/pre>\n

\u306e\u90e8\u5206\u306f\u3001Go\u3067\u5b9a\u7fa9\u3057\u305fPet\u69cb\u9020\u4f53\u306e\u51e6\u7406\u3092JavaScript\u304b\u3089\u547c\u3073\u51fa\u3059\u305f\u3081\u306e\u8a2d\u5b9a\u3067\u3059\u3002
\nJavaScript\u3067\u3001pet.New('Maru').Name()<\/code> \u3068\u3044\u3046\u8a18\u8ff0\u3067\u51e6\u7406\u3092\u547c\u3073\u51fa\u305b\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

\u5909\u63db\u3055\u308c\u305fJavaScript\u3092\u8aad\u307f\u8fbc\u3080\u51e6\u7406\u3092\u5b9f\u88c5<\/h2>\n

\u4ee5\u4e0b\u306e\u3088\u3046\u306a index.html<\/code> \u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n

<html>\n<head>\n    <script src=".\/test.js"><\/script>\n    <script src=".\/main.js"><\/script>\n<\/head>\n<\/html><\/code><\/pre>\n

test.js<\/code>\u306f\u3001Go\u30d7\u30ed\u30b0\u30e9\u30e0\u304b\u3089\u5909\u63db\u3057\u305fJavaScript\u30d5\u30a1\u30a4\u30eb\u3067\u3059\u3002<\/p>\n

main.js<\/code>\u306f\u3001test.js\u306b\u5b9a\u7fa9\u3055\u308c\u305f\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u547c\u3073\u51fa\u3059\u7528\u306eJavaScript\u30d5\u30a1\u30a4\u30eb\u3067\u3059\u3002
\n\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u3001\u5185\u5bb9\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n

alert(pet.New('Maru').Name());<\/code><\/pre>\n

Go\u3092JavaScript\u306b\u5909\u63db<\/h2>\n

\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30de\u30f3\u30c9\u3067\u3001Go\u3092JavaScript\u306b\u5909\u63db\u3057\u307e\u3059\u3002<\/p>\n

$ gopherjs build -o test.js<\/code><\/pre>\n

\u5b9f\u884c\u3059\u308b\u3068 test.js<\/code> \u304c\u51fa\u529b\u3055\u308c\u307e\u3059\u3002<\/p>\n

\u52d5\u4f5c\u78ba\u8a8d<\/h2>\n

index.html\u3092\u30d6\u30e9\u30a6\u30b6\u3067\u958b\u304f\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u52d5\u4f5c\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n

    \n
  • \u300cMaru\u300d\u3068\u3044\u3046\u6587\u5b57\u5217\u304calert\u3067\u8868\u793a\u3055\u308c\u308b<\/li>\n
  • \u300cHello world!\u300d\u3068\u3044\u3046\u6587\u5b57\u5217\u304c\u30d6\u30e9\u30a6\u30b6\u306b\u8868\u793a\u3055\u308c\u308b<\/li>\n<\/ul>\n

    \u4ee5\u4e0a\u3067\u3001Go\u3092JavaScript\u306b\u5909\u63db\u3057\u3066\u52d5\u4f5c\u78ba\u8a8d\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n

    \u307e\u3068\u3081<\/h2>\n

    GopherJS\u3067\u3001Go\u306e\u7c21\u5358\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u3092JavaScript\u306b\u5909\u63db\u3057\u3066\u3001\u30d6\u30e9\u30a6\u30b6\u4e0a\u3067\u5b9f\u884c\u3059\u308b\u3068\u3053\u308d\u307e\u3067\u3092\u89e3\u8aac\u3057\u307e\u3057\u305f\u3002
    \nGo\u3067\u66f8\u304b\u308c\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u30d6\u30e9\u30a6\u30b6\u3067\u3082\u4f7f\u7528\u3057\u305f\u3044\u5834\u5408\u306a\u3069\u306b\u4fbf\u5229\u3067\u3059\u306d\u3002\u6a5f\u4f1a\u304c\u3042\u308c\u3070\u3001\u4f7f\u3063\u3066\u3044\u304d\u305f\u3044\u3067\u3059\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u6982\u8981 \u4eca\u56de\u306f\u3001Go\u3092Web\u30d6\u30e9\u30a6\u30b6\u4e0a\u3067\u52d5\u4f5c\u3055\u305b\u308b\u305f\u3081\u306b\u3001GopherJS\u3092\u5229\u7528\u3059\u308b\u65b9\u6cd5\u3092\u89e3\u8aac\u3057\u307e\u3059\u3002 \u4ed6\u306b\u3082\u65b9\u6cd5\u3084\u30c4\u30fc\u30eb\u306f\u3042\u308a\u307e\u3059\u304c\u3001\u79c1\u304c\u8aad\u3093\u3067\u3044\u305f\u30aa\u30fc\u30d7\u30f3\u30bd\u30fc\u30b9\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u4f7f\u308f\u308c\u3066\u3044\u3066\u8208\u5473\u3092\u6301\u3063\u305f\u305f\u3081\u3001\u4eca\u56de\u306fGopherJS\u306b\u3064\u3044\u3066\u8abf\u67fb\u3057\u307e\u3057\u305f\u3002 \u516c\u5f0f\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u8aad\u3093\u3067\u3082\u3001\u3072\u3068\u307e\u305a\u52d5\u4f5c\u78ba\u8a8d\u3059\u308b\u65b9\u6cd5\u304c\u5206\u304b\u308a\u306b\u304f\u304b\u3063\u305f\u305f\u3081\u3001\u672c\u8a18\u4e8b\u3067\u306f\u3001\u7c21\u5358\u306a\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u52d5\u4f5c\u3055\u305b\u308b\u3068\u3053\u308d\u307e\u3067\u3092\u89e3\u8aac\u3057\u3066\u3044\u307e\u3059\u3002 […]<\/p>\n","protected":false},"author":5,"featured_media":1430,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[128,77],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/1429"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=1429"}],"version-history":[{"count":1,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/1429\/revisions"}],"predecessor-version":[{"id":1431,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/1429\/revisions\/1431"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/1430"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=1429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=1429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=1429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}