Initial progress

This commit is contained in:
Alex D. 2021-04-30 10:42:29 +00:00
commit e4b842666c
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
4 changed files with 76 additions and 0 deletions

22
fibonacci.go Normal file
View File

@ -0,0 +1,22 @@
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
last := []int {0, 1}
return func() int {
ret := last[0]
temp := []int {last[1], last[0] + last[1]}
last = temp
return ret
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}

18
slices.go Normal file
View File

@ -0,0 +1,18 @@
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
var ret [][]uint8 = make([][]uint8, dx);
for x := 0; x < dx; x++ {
ret[x] = make([]uint8, dy);
for y := 0; y < dy; y++ {
ret[x][y] = uint8((x+y)/2);
}
}
return ret;
}
func main() {
pic.Show(Pic)
}

17
sqrt.go Normal file
View File

@ -0,0 +1,17 @@
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
z := 1.0;
for count := 0; count < 10; count++ {
z -= (z*z - x) / (2*z);
}
return z;
}
func main() {
fmt.Println(Sqrt(2));
}

19
wc.go Normal file
View File

@ -0,0 +1,19 @@
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
var wc map[string]int = make(map[string]int)
var str[]string = strings.Fields(s)
for _, val := range str {
wc[val]++
}
return wc
}
func main() {
wc.Test(WordCount)
}