Skip to content

Instantly share code, notes, and snippets.

View rolaveric's full-sized avatar

Jason Stone rolaveric

View GitHub Profile
@rolaveric
rolaveric / typesEmptyInterface.go
Created March 2, 2014 07:22
Example of using an empty interface to work around the type system in Go
// X needs anything
func X(anything interface{}) {
// Nothing comes for free - need to see what I got
switch value := anything.(type) {
case string:
fmt.Printf("I got a string!: %s", value)
default:
fmt.Print("I got... something!")
}
}
@rolaveric
rolaveric / typesEmptyInterfaceSlice.go
Created March 2, 2014 07:34
Example of using an empty interface slice to hold pointers to another slice in Go
cols, err := rows.Cols()
// rows.Scan wants '[]interface{}' as an argument, but we want the values
// loaded into a '[]string'. So we must copy the references into such a slice
// See http://code.google.com/p/go-wiki/wiki/InterfaceSlice for details
scanArgs := make([]interface{}, len(cols))
values := make([]string, len(cols))
for i := range values {
scanArgs[i] = &values[i]
}
@rolaveric
rolaveric / gopherjsIntroMain.go
Last active September 28, 2017 15:39
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,
@rolaveric
rolaveric / gopherjsResult.js
Created March 7, 2014 08:19
The result from building gopherjsIntroMain.go with GopherJS
"use strict";
(function() {
Error.stackTraceLimit = -1;
var go$reservedKeywords = ["abstract", "arguments", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "debugger", "default", "delete", "do", "double", "else", "enum", "eval", "export", "extends", "false", "final", "finally", "float", "for", "function", "goto", "if", "implements", "import", "in", "instanceof", "int", "interface", "let", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try", "typeof", "var", "void", "volatile", "while", "with", "yield"];
var go$global;
if (typeof window !== "undefined") {
go$global = window;
@rolaveric
rolaveric / gopherjsResultShort.js
Last active August 29, 2015 13:57
The extract from gopherjsResult.js which doesn't include the builtin library code.
// Lines 8-13
var go$global;
if (typeof window !== "undefined") {
go$global = window;
} else if (typeof GLOBAL !== "undefined") {
go$global = GLOBAL;
}
// Lines 1425-1461
go$packages["github.com/rolaveric/gopherjs/pet"] = (function() {
@rolaveric
rolaveric / gopherjsModelOriginal.js
Last active August 29, 2015 13:57
Example of an existing legacy Javascript object we want to port to Go.
// User Type
function User(name, id) {
this.name = name;
this.id = id;
this.save = function () {
DB.query('UPDATE User SET name = ? WHERE id = ?', this.name, this.id);
}
}
@rolaveric
rolaveric / gopherjsModel.js
Last active August 29, 2015 13:57
Example of gopherjsModelOriginal.js refactored to better match a Go API.
// Namespace created within an IIFE for private scope
var user = (function () {
// Variable for holding the injected DB interface
var DB;
// User Type
function User(name, id) {/* ... */}
return {
// Expose a function for setting the DB interface
@rolaveric
rolaveric / gopherjsModel.go
Last active August 29, 2015 13:57
Example of a Javascript model object converted to Go
package user
// Interface for a database result row
type DBRow interface {
GetInt(colnum int) int
GetString(colnum int) string
}
// Interface for a database result
type DBResult interface {
@rolaveric
rolaveric / angularBoilerplate.dart
Created March 29, 2014 04:50
Most basic boilerplate required for an AngularDart application.
import 'package:angular/angular.dart';
main() {
ngBootstrap();
// ...
}
@rolaveric
rolaveric / angularDirective.html
Created March 29, 2014 05:33
HTML changed from using an "id" attribute to a custom "sample-text-id" attribute.
<div id="sample_container_id">
<p sample-text-id>Click me!</p>
</div>