Initial commit - learning the basics
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
module learning/tour
|
||||
|
||||
go 1.24.5
|
||||
|
||||
require rsc.io/quote v1.5.2
|
||||
|
||||
require (
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c // indirect
|
||||
rsc.io/sampler v1.3.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3Y=
|
||||
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
|
||||
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
|
||||
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
|
||||
@@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "rsc.io/quote"
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello Go!")
|
||||
fmt.Println(quote.Go())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/cmplx"
|
||||
)
|
||||
|
||||
var c, java, python bool
|
||||
var n, m int = 1, 2
|
||||
var (
|
||||
ToBe bool = false
|
||||
MaxInt uint64 = 1<<64 - 1
|
||||
z complex128 = cmplx.Sqrt(-5 + 12i)
|
||||
)
|
||||
|
||||
func add(x, y int) int {
|
||||
return x + y
|
||||
}
|
||||
|
||||
func swap (x, y string) (string, string) {
|
||||
return y, x
|
||||
}
|
||||
|
||||
func split(sum int) (x, y int) {
|
||||
x = sum * 4 / 9
|
||||
y = sum - x
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(add(42,13))
|
||||
x := [3]int{2,1,1}
|
||||
fmt.Println(x)
|
||||
|
||||
a, b := swap("hello", "go")
|
||||
fmt.Println(a, b)
|
||||
|
||||
fmt.Println(split(17))
|
||||
|
||||
var i int
|
||||
fmt.Println(i, c, python, java)
|
||||
|
||||
c, java, python := true, true, "no!"
|
||||
k := 3
|
||||
fmt.Println(k, n, m, c, python, java)
|
||||
|
||||
fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
|
||||
fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
|
||||
fmt.Printf("Type: %T Value: %v\n", z, z)
|
||||
|
||||
g := 42
|
||||
f := float64(g)
|
||||
u := uint(f)
|
||||
fmt.Println(g, f, u)
|
||||
|
||||
const Pi = 3.14
|
||||
const World = "世界"
|
||||
const Truth = true
|
||||
|
||||
fmt.Println("Hello", World)
|
||||
fmt.Println("Happy", Pi, "Day")
|
||||
fmt.Println("Go rules?", Truth)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"math"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("My favorite number is", rand.Intn(10))
|
||||
fmt.Println(math.Pi)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func Sqrt(x float64) float64 {
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(Sqrt(2))
|
||||
}
|
||||
@@ -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),
|
||||
)
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
@@ -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!")
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func printSlice(s []int) {
|
||||
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
|
||||
}
|
||||
func main() {
|
||||
var s []int
|
||||
printSlice(s)
|
||||
|
||||
s = append(s, 0)
|
||||
printSlice(s)
|
||||
|
||||
s = append(s, 1)
|
||||
printSlice(s)
|
||||
|
||||
s = append(s, 2, 3, 4)
|
||||
printSlice(s)
|
||||
|
||||
s = append(s, 56)
|
||||
printSlice(s)
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
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)
|
||||
|
||||
// Slice literals - like array literals, but without the length
|
||||
q := []int{2, 3, 5, 7, 11, 13}
|
||||
fmt.Println(q)
|
||||
|
||||
r := []bool{true, false, true, true, false, true}
|
||||
fmt.Println(r)
|
||||
|
||||
sx := []struct {
|
||||
i int
|
||||
b bool
|
||||
}{
|
||||
{2, true},
|
||||
{3, false},
|
||||
{5, true},
|
||||
{7, true},
|
||||
{11, false},
|
||||
{13, true},
|
||||
}
|
||||
fmt.Println(sx)
|
||||
|
||||
// Slice defaults
|
||||
|
||||
ss := []int{2, 3, 5, 7, 11, 13}
|
||||
|
||||
ss = ss[1: 4]
|
||||
fmt.Println(ss)
|
||||
|
||||
ss = ss[:2]
|
||||
fmt.Println(ss)
|
||||
|
||||
ss = ss[1:]
|
||||
fmt.Println(ss)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func printSlice(s []int) {
|
||||
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
|
||||
}
|
||||
|
||||
func main() {
|
||||
a := make([]int, 5)
|
||||
printSlice(a)
|
||||
|
||||
b := make([]int, 0, 5)
|
||||
printSlice(b)
|
||||
|
||||
c := b[:2]
|
||||
printSlice(c)
|
||||
|
||||
d := c[2:5]
|
||||
printSlice(d)
|
||||
|
||||
// Slices can contain any type including other slices
|
||||
board := [][]string{
|
||||
[]string{"_", "_", "_"},
|
||||
[]string{"_", "_", "_"},
|
||||
[]string{"_", "_", "_"},
|
||||
}
|
||||
|
||||
board[0][0] = "X"
|
||||
board[2][2] = "O"
|
||||
board[1][2] = "X"
|
||||
board[1][0] = "O"
|
||||
board[0][2] = "X"
|
||||
|
||||
for i := 0; i < len(board); i++ {
|
||||
fmt.Printf("%s\n", strings.Join(board[i], " "))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
i, j := 42, 2701
|
||||
|
||||
p := &i
|
||||
fmt.Println(*p)
|
||||
*p = 21
|
||||
fmt.Println(i)
|
||||
|
||||
p = &j
|
||||
*p = *p / 37
|
||||
fmt.Println(j)
|
||||
|
||||
fmt.Println(i, j)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}
|
||||
|
||||
func main() {
|
||||
for i, v := range pow {
|
||||
fmt.Printf("2**%d = %d\n", i, v)
|
||||
}
|
||||
|
||||
// You can skip the index by assigning it to "_"
|
||||
// Or you can skip the value by omitting the 2nd value -> "for i := range XXX"
|
||||
new_pow := make([]int, 10)
|
||||
for i := range new_pow {
|
||||
new_pow[i] = 1 << uint(i)
|
||||
}
|
||||
for _, value := range new_pow {
|
||||
fmt.Printf("%d\n", value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func printSlice(s []int) {
|
||||
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := []int{2, 3, 5, 7, 11, 13}
|
||||
printSlice(s)
|
||||
|
||||
s = s[:0]
|
||||
printSlice(s)
|
||||
|
||||
s = s[:4]
|
||||
printSlice(s)
|
||||
|
||||
s = s[2:]
|
||||
printSlice(s)
|
||||
|
||||
// Extend beyond capacity
|
||||
// s = s[:10]
|
||||
// printSlice(s)
|
||||
|
||||
// Zero value of a slice is nil
|
||||
var ns []int
|
||||
printSlice(ns)
|
||||
|
||||
if ns == nil {
|
||||
fmt.Println("nil!!")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Vertex struct {
|
||||
X int
|
||||
Y int
|
||||
}
|
||||
|
||||
var (
|
||||
v1 = Vertex{1, 2}
|
||||
v2 = Vertex{X: 1}
|
||||
v3 = Vertex{}
|
||||
p = &Vertex{1, 2}
|
||||
)
|
||||
|
||||
func main() {
|
||||
v := Vertex{1, 2}
|
||||
v.X = 4
|
||||
fmt.Println(v)
|
||||
|
||||
o := &v
|
||||
o.X = 12
|
||||
fmt.Printf("New vertex value is %d\n", v)
|
||||
|
||||
fmt.Println(v1, p, v2, v3)
|
||||
}
|
||||
Reference in New Issue
Block a user