Initial commit - learning the basics

This commit is contained in:
V
2025-08-30 13:55:18 +01:00
commit 194ca9b7a9
27 changed files with 830 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
}
func main() {
fmt.Println(Sqrt(2))
}
+22
View File
@@ -0,0 +1,22 @@
package main
import (
"fmt"
"math"
)
func pow(x, n, lim float64) float64 {
if v := math.Pow(x, n); v < lim {
return v
} else {
fmt.Printf("%g >= %g\n", v, lim)
}
return lim
}
func main() {
fmt.Println(
pow(3, 2, 10),
pow(3, 3, 20),
)
}
+32
View File
@@ -0,0 +1,32 @@
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
precision := 0.00001
old_z := 0.0
z := x
iter := 0
for (z - old_z) > precision {
iter += 1
old_z := z
z -= (z*z - x) / (2*z)
if old_z - z < 0 {
if ((old_z - z) * -1) < precision {
fmt.Println("Required precision has been reached after", iter, "iterations! Square root of", x , "is", z)
return z
}
} else if (old_z - z) < precision {
fmt.Println("Required precision has been reached after", iter, "iterations! Square root of", x , "is", z)
return z
}
}
return z
}
func main() {
fmt.Println(Sqrt(2))
fmt.Println(Sqrt(29))
}
+29
View File
@@ -0,0 +1,29 @@
package main
import (
"fmt"
"math"
)
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}
return fmt.Sprint(math.Sqrt(x))
}
func main() {
sum := 0
for i := 0; i < 10; i++ {
sum += 1
}
fmt.Println(sum)
sum_2 := 1
for sum_2 < 1000 {
sum_2 += sum_2
}
fmt.Println(sum_2)
fmt.Println(sqrt(2), sqrt(-4))
}
+55
View File
@@ -0,0 +1,55 @@
package main
import (
"fmt"
"time"
"runtime"
)
func letsDefer(text string) {
defer fmt.Println(text)
fmt.Print("I have deferred: ")
}
func main() {
fmt.Print("Go runs on ")
switch os := runtime.GOOS; os {
case "darwin":
fmt.Println("macOS.")
case "linux":
fmt.Println("Linux.")
default:
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.")
}
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.")
}
letsDefer("Some stuff")
fmt.Println("Counting...")
for i:=0; i<10; i++ {
defer fmt.Println(i)
}
fmt.Println("Done!")
}