Skip to content

Instantly share code, notes, and snippets.

@shockalotti
shockalotti / Go Golang - channel select example
Last active August 29, 2015 14:02
Go Golang - channel select example
package main
import (
"fmt"
"time"
)
func main() {
c1 := make(chan string, 1)
c2 := make(chan string, 1)
@shockalotti
shockalotti / Go Golang - goroutines, channels, channel direction - ping pong example
Created June 4, 2014 03:52
Go Golang - goroutines, channels, channel direction - ping pong example
package main
import (
"fmt"
"time"
)
func pinger(c chan string) {
for i := 0; ; i++ {
c <- "ping"
@shockalotti
shockalotti / Go Golang - structs, methods, embedded types, interfaces example
Last active November 4, 2019 17:20
Go Golang - structs, methods, embedded types, interfaces example
package main
import ("fmt"; "math")
// interfaces
type Shaper interface {
area() float64
perimeter() float64
}
@shockalotti
shockalotti / Go Golang - pointers exercise, swap x and y
Created May 29, 2014 05:27
Go Golang - pointers exercise, swap x and y
package main
import "fmt"
func swap(px, py *int) {
tempx := *px
tempy := *py
*px = tempy
*py = tempx
}
@shockalotti
shockalotti / Go Golang - recursive function, fibonacci sequence
Created May 28, 2014 03:03
Go Golang - recursive function, fibonacci sequence
package main
import "fmt"
func fib(n uint) uint {
if n == 0 {
return 0
} else if n == 1 {
return 1
} else {
@shockalotti
shockalotti / Go Golang - variadic function, find greatest number in list
Created May 28, 2014 02:53
Go Golang - variadic function, find greatest number in list
package main
import "fmt"
func greatestNumber(args ... int) int {
max := int(0)
for _, arg := range args {
if arg > max {
max = arg
}
@shockalotti
shockalotti / Go Golang - halve integer
Created May 28, 2014 02:20
Go Golang - halve integer
package main
import "fmt"
func halfNumber(x uint) (uint, bool){
if x % 2 == 0 {
return x/2, true
} else {
return x/2, false
}
@shockalotti
shockalotti / Go Golang - slice sum function
Created May 28, 2014 02:02
Go Golang - slice sum function
package main
import "fmt"
func sumSlices(x[]float64, y[]float64) float64 {
totalx := 0.0
for _, valuex := range x {
totalx += valuex
}
@shockalotti
shockalotti / Go Golang - Defer, Panic function example
Created May 28, 2014 01:45
Go Golang - Defer, Panic function example
package main
import "fmt"
func main() {
defer func() {
str := recover()
fmt.Println(str)
}()
panic("PANIC")
@shockalotti
shockalotti / Go Golang - basic Defer function
Created May 28, 2014 01:38
Go Golang - basic Defer function
package main
import "fmt"
func first() {
fmt.Println("1st")
}
func second() {
fmt.Println("2nd")
}