Skip to content

Instantly share code, notes, and snippets.

@jochasinga
jochasinga / func.go
Created September 30, 2014 20:27
Function in Go
package main
import "fmt"
func colormap(r uint8, g uint8, b uint8) (color map[string]uint8) {
color := map[string]uint8{
"r" : r
"g" : g,
"b" : b,
}
@jochasinga
jochasinga / swap.go
Created September 30, 2014 20:48
Multiple return values in Go
package main
import "fmt"
func swap(a, b int) (int, int) {
_a := a
_b := b
a, b = _b, _a
@jochasinga
jochasinga / file.go
Created September 30, 2014 21:23
Using defer to close file in Go
package main
import "os"
func main() {
file, err := os.Open("test.txt")
defer file.Close()
if err != nil {
// handle the error here
return "", err
@jochasinga
jochasinga / panic_recover.go
Created September 30, 2014 21:37
Defer-panic-recover suite in Go
package main
import "fmt"
func main() {
// makes sure recover gets call when faced with panic
defer func() {
str := recover()
fmt.Println(str)
}()
@jochasinga
jochasinga / struct.go
Last active August 29, 2015 14:07
Struct in Go
package main
import "fmt"
type Gopher struct {
kingdom string
color map[string]int
num_legs int
height float
}
@jochasinga
jochasinga / struct_func.go
Last active August 29, 2015 14:07
Function that takes struct as an argument in Go
package main
import "fmt"
type Gopher struct {
kingdom string
color map[string]int
num_legs int
height float32
}
@jochasinga
jochasinga / interface1.go
Last active August 29, 2015 14:07
Basic method calling before implementing interfaces
package main
import "fmt"
type Man struct {
language string
// other factors to decide if the girl would like you (for fun)
name, nationality, build, eye_color string
age, height uint8
@jochasinga
jochasinga / interface2.go
Created October 1, 2014 18:08
Abstracting a method to an interface
package main
import "fmt"
type Greeter interface {
Greet()
}
type Man struct {
language string
@jochasinga
jochasinga / interface3.go
Last active August 29, 2015 14:07
Inheriting and reusing through structs and interfaces in Go
package main
import(
"fmt"
"io/ioutil"
"os"
)
type Greeter interface {
Greet()
@jochasinga
jochasinga / pot.js
Last active August 29, 2015 14:07
Johnny-five potentiometer code for adjusting RGB LED colors and updating values to Firebase
var Firebase = require("firebase");
var five = require("johnny-five");
// Create a new reference of Firebase db
var firebaseRef = new Firebase(
// fictional URL, replace it with your own from Firebase
"https://burning-limbo-6666.firebaseio.com/colors"
);
five.Board().on("ready", function() {