This commit is contained in:
V 2025-08-30 19:14:01 +01:00
parent 194ca9b7a9
commit cfd7ab8a0b
10 changed files with 179 additions and 39 deletions

View File

@ -1,39 +0,0 @@
package main
import "fmt"
func main() {
var aa [2]string
aa[0] = "Hello"
aa[1] = "World"
fmt.Println(aa[0], aa[1])
fmt.Println(aa)
primes := [6]int{2, 3, 5, 7, 11, 13}
fmt.Println(primes)
var s []int = primes[1:4]
fmt.Printf("The primes are %d and the slice is %d", primes, s)
// Slices are references to array sections
// Changing elements in a slice results in changes to the array
// So other slices referencing the same array section will also be changed
names := [4]string{
"John",
"Paul",
"George",
"Ringo",
}
fmt.Println(names)
a := names[0:2]
b := names[1:3]
fmt.Println(a, b)
// Now make some changes
b[0] = "XXX"
fmt.Println(a, b)
fmt.Println(names)
}

View File

@ -0,0 +1,28 @@
package main
import "fmt"
func fibonacci() func() int {
previous := 0
current := 0
result := 0
return func() int {
if current == 0 {
current = 1
} else if current == 1 && result == 0 {
result = current
} else {
result = previous + current
previous = current
current = result
}
return result
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}

View File

@ -0,0 +1,26 @@
package main
import (
"golang.org/x/tour/wc"
"strings"
)
func WordCount(s string) map[string]int {
result := map[string]int{}
fields := strings.Fields(s)
for i := range fields {
word := fields[i]
_, exists := result[word]
if exists == true {
result[word] += 1
} else {
result[word] = 1
}
}
return result
}
func main() {
wc.Test(WordCount)
}

View File

@ -0,0 +1,26 @@
package main
import (
"golang.org/x/tour/pic"
"fmt"
)
func Pic(dx, dy int) [][]uint8 {
pic := make([][]uint8, dy)
for i := range(dy) {
inner_slice := make([]uint8, dx)
for y := range(dx) {
res := i * y
inner_slice[y] = uint8(res)
}
pic[i] = inner_slice
}
return pic
}
func main() {
s := Pic(8, 8)
fmt.Println(s)
pic.Show(Pic)
}

View File

@ -0,0 +1,20 @@
package main
import "fmt"
func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}
func main() {
pos, neg := adder(), adder()
for i := 0; i < 10; i++ {
fmt.Println(
pos(i), neg(-2*i),
)
}
}

View File

@ -0,0 +1,21 @@
package main
import (
"fmt"
"math"
)
func compute(fn func(float64, float64) float64) float64 {
return fn(3,4)
}
func main() {
hypot := func(x, y float64) float64 {
return math.Sqrt(x*x + y*y)
}
fmt.Println(hypot(5, 12))
fmt.Println(compute(hypot))
fmt.Println(compute(math.Pow))
}

5
tour_of_go/l4/go.mod Normal file
View File

@ -0,0 +1,5 @@
module exercise-maps.go
go 1.24.5
require golang.org/x/tour v0.1.0

2
tour_of_go/l4/go.sum Normal file
View File

@ -0,0 +1,2 @@
golang.org/x/tour v0.1.0 h1:OWzbINRoGf1wwBhKdFDpYwM88NM0d1SL/Nj6PagS6YE=
golang.org/x/tour v0.1.0/go.mod h1:DUZC6G8mR1AXgXy73r8qt/G5RsefKIlSj6jBMc8b9Wc=

35
tour_of_go/l4/maps.go Normal file
View File

@ -0,0 +1,35 @@
package main
import "fmt"
type Vertex struct {
Lat, Long float64
}
var m map[string]Vertex
var new_m = map[string]Vertex{
"Bell Labs": Vertex{
40.68433, -74.39967,
},
"Google": Vertex{
37.42202, -122.08408,
},
}
// The above can also be written
var new_new_m = map[string]Vertex{
"Bell Labs": {40.68433, -74.39967},
"Google": {37.42202, -122.08408},
}
func main() {
m = make(map[string]Vertex)
m["Bell Labs"] = Vertex{
40.68433, -74.39967,
}
fmt.Println(m["Bell Labs"])
fmt.Println(new_m)
fmt.Println(new_new_m)
}

View File

@ -0,0 +1,16 @@
package main
import "fmt"
runc main() {
m := make(map[string]int)
m["Answer"] = 42
fmt.Println("The value:", m["Answer"])
delete(m, "Answer")
fmt.Println("The value:", m["Answer"])
v, ok := m["Answer"]
fmt.Println("The value:", v, "Present?", ok)
}