Skip to content

Instantly share code, notes, and snippets.

@cryptix
Created June 10, 2014 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save cryptix/054b955e55f144428f97 to your computer and use it in GitHub Desktop.
Save cryptix/054b955e55f144428f97 to your computer and use it in GitHub Desktop.
GopherJS utf8 encoding problem
<html>
<head>
<meta charset="utf-8">
<title>test</title>
</head>
<script src="main.js"></script>
<script>
function mkListInJs (id) {
var div = document.getElementById(id);
var ul = document.createElement("ul");
var data = myLibrary.directDataAccess;
for (var i = data.length - 1; i >= 0; i--) {
var s = data[i];
var li = document.createElement("li");
li.innerHTML = s.Num + " - " + s.Str;
ul.appendChild(li);
}
div.appendChild(ul);
}
function ready() {
mkListInJs("inJs");
myLibrary.mkListInGo("inGo");
}
</script>
<body onload="ready();">
<h1>Made in Go</h1>
<div id="inGo"></div>
<h1>Made in Js</h1>
<div id="inJs"></div>
</body>
</html>
package main
import (
"fmt"
"github.com/gopherjs/gopherjs/js"
"honnef.co/go/js/dom"
)
type myStruct struct {
Str string
Num int
}
var myData []myStruct
func main() {
myData = make([]myStruct, 5)
myData[0] = myStruct{"Hello World™", 42}
myData[1] = myStruct{"Hello World™", 23}
myData[2] = myStruct{"Hello World™", 64}
myData[3] = myStruct{"Hello World™", 128}
myData[4] = myStruct{"Hello World™", 256}
js.Global.Set("myLibrary", map[string]interface{}{
"mkListInGo": mkListInGo,
"directDataAccess": myData,
})
}
func mkListInGo(id string) {
d := dom.GetWindow().Document()
div := d.GetElementByID(id)
ul := d.CreateElement("ul")
for _, s := range myData {
li := d.CreateElement("li")
li.SetInnerHTML(fmt.Sprintf("%d - %s", s.Num, s.Str))
ul.AppendChild(li)
}
div.AppendChild(ul)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment