diff --git a/closures.go b/closures.go new file mode 100644 index 0000000..688aff3 --- /dev/null +++ b/closures.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "math/rand" + "time" +) + +func intSeq() func() int { + i := 0 + return func() int { + i++ + return i + } +} + +func words() func() string { + wordlist := [11]string{1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six", + 7: "seven", 8: "eight", 9: "nine", 10: "ten"} + + return func() string { + rand.Seed(time.Now().UnixNano()) + min := 0 + max := 10 + return wordlist[rand.Intn(max-min+1)+min] + } +} + +func main() { + + nextInt := intSeq() + + fmt.Println(nextInt()) + fmt.Println(nextInt()) + fmt.Println(nextInt()) + + newInts := intSeq() + fmt.Println(newInts()) + + newWords := words() + fmt.Println(newWords()) + +} diff --git a/funcs.go b/funcs.go new file mode 100644 index 0000000..2a7bc2a --- /dev/null +++ b/funcs.go @@ -0,0 +1,28 @@ +package main + +import "fmt" + +func plus(a int, b int) int { + return a + b +} + +func plusPlus(a, b, c int) int { + return a + b + c +} + +func concat(a, b string) string { + return a + b +} + +func main() { + + res := plus(1, 2) + fmt.Println("1+2 =", res) + + res = plusPlus(1, 2, 3) + fmt.Println("1+2+3 =", res) + + words := concat("howdy", "doody") + fmt.Println(words) + +} diff --git a/methods.go b/methods.go new file mode 100644 index 0000000..b76681f --- /dev/null +++ b/methods.go @@ -0,0 +1,26 @@ +package main + +import "fmt" + +type rect struct { + width, height int +} + +func (r *rect) area() int { + return r.width * r.height +} + +func (r rect) perim() int { + return 2*r.width + 2*r.height +} + +func main() { + r := rect{width: 10, height: 5} + + fmt.Println("area: ", r.area()) + fmt.Println("perim:", r.perim()) + + rp := &r + fmt.Println("area: ", rp.area()) + fmt.Println("perim:", rp.perim()) +} diff --git a/multiple-returns.go b/multiple-returns.go new file mode 100644 index 0000000..5861987 --- /dev/null +++ b/multiple-returns.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "strings" +) + +func vals() (int, int) { + return 3, 7 +} + +func strs(a string) (string, string, string) { + b := strings.Split(a, " ") + return b[0], b[1], b[2] +} + +func main() { + + a, b := vals() + fmt.Println(a) + fmt.Println(b) + + _, c := vals() + fmt.Println(c) + + x, y, z := strs("One Two Three") + fmt.Println(x, y, z) + +} diff --git a/recursed.go b/recursed.go new file mode 100644 index 0000000..149bed8 --- /dev/null +++ b/recursed.go @@ -0,0 +1,18 @@ +package main + +import "fmt" + +func fact(n int) int { + if n == 0 { + return 1 + } + return n * fact(n-1) +} + +func main() { + fmt.Println(fact(7)) + fmt.Println(fact(129)) + fmt.Println(fact(11)) + fmt.Println(fact(1)) + fmt.Println(fact(9321)) +} diff --git a/structs.go b/structs.go new file mode 100644 index 0000000..b0fcdeb --- /dev/null +++ b/structs.go @@ -0,0 +1,31 @@ +package main + +import "fmt" + +type person struct { + name string + age int +} + +func newPerson(name string) *person { + p := person{name: name} + p.age = 42 + return &p +} + +func main() { + fmt.Println(person{"Bob", 20}) + fmt.Println(person{name: "Alice", age: 30}) + fmt.Println(person{name: "Fred"}) + fmt.Println(&person{name: "Ann", age: 40}) + fmt.Println(newPerson("Jon")) + + s := person{name: "Sean", age: 50} + fmt.Println(s.name) + + sp := &s + fmt.Println(sp.age) + + sp.age = 51 + fmt.Println(sp.age) +} diff --git a/variadic.go b/variadic.go new file mode 100644 index 0000000..6d787a0 --- /dev/null +++ b/variadic.go @@ -0,0 +1,31 @@ +package main + +import "fmt" + +func sum(nums ...int) { + fmt.Print(nums, " ") + total := 0 + for _, num := range nums { + total += num + } + fmt.Println(total) +} + +func sentence(words ...string) { + snt := "" + for _, word := range words { + snt += word + " " + } + fmt.Println(snt) +} + +func main() { + + sum(1, 2) + sum(1, 2, 3) + + nums := []int{1, 2, 3, 4} + sum(nums...) + + sentence("This", "is", "my", "useless", "function") +}