Created
June 10, 2014 18:23
-
-
Save cryptix/054b955e55f144428f97 to your computer and use it in GitHub Desktop.
GopherJS utf8 encoding problem
This file contains 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
<html> | |
<head> | |
<title>test</title> | |
</head> | |
<script src="main.js"></script> | |
<script>document.write(myLibrary.makeHtml())</script> | |
<script> | |
document.write("<h1>From JS</h1><ul>"); | |
var d = myLibrary.uncleanData; | |
for (var i = d.length - 1; i >= 0; i--) { | |
document.write("<li>"+d[i].Num+"-"+d[i].Str+"</li>"); | |
}; | |
document.write("</ul>"); | |
</script> | |
</html> |
This file contains 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 ( | |
"bytes" | |
"github.com/gopherjs/gopherjs/js" | |
"html/template" | |
) | |
type myStruct struct { | |
Str string | |
Num int | |
} | |
const myListTmpl = ` | |
<h1>From html/template</h1> | |
<ul> | |
{{range .}} | |
<li> | |
{{.Num}}-{{.Str}} | |
</li> | |
{{end}} | |
</ul>` | |
var listTpl = template.Must(template.New("test").Parse(myListTmpl)) | |
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{}{ | |
"makeHtml": makeHtml, | |
"uncleanData": myData, | |
}) | |
} | |
func makeHtml() string { | |
var buf bytes.Buffer | |
err := listTpl.Execute(&buf, myData) | |
if err != nil { | |
panic(err) | |
} | |
return buf.String() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment