add misplaced scripts

This commit is contained in:
Greg Gauthier 2021-03-04 16:57:36 +00:00
parent c580618e36
commit d635233a20
7 changed files with 206 additions and 0 deletions

43
closures.go Normal file
View File

@ -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())
}

28
funcs.go Normal file
View File

@ -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)
}

26
methods.go Normal file
View File

@ -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())
}

29
multiple-returns.go Normal file
View File

@ -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)
}

18
recursed.go Normal file
View File

@ -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))
}

31
structs.go Normal file
View File

@ -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)
}

31
variadic.go Normal file
View File

@ -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")
}