Skip to content

Instantly share code, notes, and snippets.

@blitzblade
Last active January 7, 2021 05:24
Show Gist options
  • Save blitzblade/2ca045fd5f4c69169f9519c7cfc7934a to your computer and use it in GitHub Desktop.
Save blitzblade/2ca045fd5f4c69169f9519c7cfc7934a to your computer and use it in GitHub Desktop.
#go
run go docs locally:
godoc -http=:6060
package main
import (
"fmt"
"math"
"runtime"
"strings"
"time"
)
const (
// Create a huge number by shifting a 1 bit left 100 places.
// In other words, the binary number that is 1 followed by 100 zeroes.
Big = 1 << 100
// Shift it right again 99 places, so we end up with 1<<1, or 2.
Small = Big >> 99
)
func add(x int, y int) int {
return x + y
}
func swap(x, y string) (string, string) {
return y, x
}
func needInt(x int) int { return x*10 + 1 }
func needFloat(x float64) float64 {
return x * 0.1
}
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}
return fmt.Sprint(math.Sqrt(x))
}
type Vertex struct {
Lat, Long float64
}
func printSlice(s []int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
func Pic(dx, dy int) [][]uint8 {
s := make([][]uint8, dy)
for i := 0; i < cap(s); i++ {
s[i] = make([]uint8, dx)
for j := 0; j < cap(s[i]); j++ {
//s[i][j] = uint8((i+j)/2)
//s[i][j] = uint8(i*j)
s[i][j] = uint8(i ^ j)
}
}
return s
}
func WordCount(s string) map[string]int {
s_arr := strings.Fields(s)
mp := make(map[string]int)
for _, v := range s_arr {
_, exists := mp[v]
if exists {
mp[v]++
} else {
mp[v] = 1
}
}
return mp
}
func main() {
a, b := swap("hello", "world")
g := 0.867 + 0.5i // complex128
fmt.Println(swap(a, b))
fmt.Println(add(42, 13))
fmt.Println(g)
fmt.Println(needInt(Small))
fmt.Println(needFloat(Small))
fmt.Println(needFloat(Big))
fmt.Print("Go runs on ")
switch os := runtime.GOOS; os {
case "darwin":
fmt.Println("OS X.")
case "linux":
fmt.Println("Linux.")
default:
// freebsd, openbsd,
// plan9, windows...
fmt.Printf("%s.\n", os)
}
fmt.Println("When's Saturday?")
today := time.Now().Weekday()
switch time.Saturday {
case today + 0:
fmt.Println("Today.")
case today + 1:
fmt.Println("Tomorrow.")
case today + 2:
fmt.Println("In two days.")
default:
fmt.Println("Too far away.")
}
//clean way to write long if-else statements
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("Good morning!")
case t.Hour() < 17:
fmt.Println("Good afternoon.")
default:
fmt.Println("Good evening.")
}
//defering till functions around return. LIFO is used to execute stacked deferred calls
fmt.Println("counting")
for i := 0; i < 10; i++ {
defer fmt.Println(i)
}
fmt.Println("done")
//pointers
i, j := 42, 2701
p := &i // point to i
fmt.Println(*p) // read i through the pointer
*p = 21 // set i through the pointer
fmt.Println(i) // see the new value of i
p = &j // point to j
*p = *p / 37 // divide j through the pointer
fmt.Println(j) // see the new value of j
//struct
v := Vertex{1, 2}
fmt.Println(v.Lat)
fmt.Println(v.Long)
//arrays
var arr [2]string
arr[0] = "Hello"
arr[1] = "World"
fmt.Println(arr[0], arr[1])
fmt.Println(arr)
primes := [6]int{2, 3, 5, 7, 11, 13}
fmt.Println(primes)
//slice of structs
s := []struct {
i int
b bool
}{
{2, true},
{3, false},
{5, true},
{7, true},
{11, false},
{13, true},
}
fmt.Println(s)
// Create a tic-tac-toe board.
board := [][]string{
[]string{"_", "_", "_"},
[]string{"_", "_", "_"},
[]string{"_", "_", "_"},
}
// The players take turns.
board[0][0] = "X"
board[2][2] = "O"
board[1][2] = "X"
board[1][0] = "O"
board[0][2] = "X"
for i := 0; i < len(board); i++ {
fmt.Printf("%s\n", strings.Join(board[i], " "))
}
// using make
// b := make([]int, 0, 5)
// printSlice("b", b)
var sk []int
printSlice(sk)
// append works on nil slices.
sk = append(sk, 0)
printSlice(sk)
// The slice grows as needed.
sk = append(sk, 1)
printSlice(sk)
// We can add more than one element at a time.
sk = append(sk, 2, 3, 4)
printSlice(sk)
//range
var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
for i, v := range pow {
fmt.Printf("2**%d = %d\n", i, v)
}
//magic picture function
//pic.Show(Pic)
//maps
var m map[string]Vertex
m = make(map[string]Vertex)
m["Bell Labs"] = Vertex{
40.68433, -74.39967,
}
var n = map[string]Vertex{
"Bell Labs": Vertex{
40.68433, -74.39967,
},
"Google": Vertex{
37.42202, -122.08408,
},
}
fmt.Println(m["Bell Labs"])
fmt.Println(n)
//mutating maps
k := make(map[string]int)
k["Answer"] = 42
fmt.Println("The value:", k["Answer"])
k["Answer"] = 48
fmt.Println("The value:", k["Answer"])
delete(k, "Answer")
fmt.Println("The value:", k["Answer"])
u, ok := k["Answer"]
fmt.Println("The value:", u, "Present?", ok)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment