feat: init & playaround
This commit is contained in:
38
go-by-example/arrays/arrays.go
Normal file
38
go-by-example/arrays/arrays.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
|
||||
var a [5]int
|
||||
fmt.Println("emp:", a)
|
||||
|
||||
a[4] = 100
|
||||
fmt.Println("set:", a)
|
||||
fmt.Println("get:", a[4])
|
||||
|
||||
fmt.Println("len:", len(a))
|
||||
|
||||
b := [5]int{1, 2, 3, 4, 5}
|
||||
fmt.Println("dcl:", b)
|
||||
// Compiler counts for you.
|
||||
b = [...]int{1, 2, 3, 4, 5}
|
||||
fmt.Println("dcl:", b)
|
||||
|
||||
b = [...]int{100, 3: 400, 500}
|
||||
fmt.Println("idx:", b)
|
||||
|
||||
var twoD [2][3]int
|
||||
for i := range 2 {
|
||||
for j := range 3 {
|
||||
twoD[i][j] = i + j
|
||||
}
|
||||
}
|
||||
fmt.Println("2d: ", twoD)
|
||||
|
||||
twoD = [2][3]int{
|
||||
{1, 2, 3},
|
||||
{1, 2, 3},
|
||||
}
|
||||
fmt.Println("2d: ", twoD)
|
||||
}
|
||||
22
go-by-example/constants/constants.go
Normal file
22
go-by-example/constants/constants.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
const s string = "constant"
|
||||
|
||||
func main() {
|
||||
fmt.Println(s)
|
||||
|
||||
const n = 500000000
|
||||
|
||||
const d = 3e20 / n
|
||||
|
||||
fmt.Println(d)
|
||||
|
||||
fmt.Println(int64(d))
|
||||
|
||||
fmt.Println(math.Sin(n))
|
||||
}
|
||||
35
go-by-example/for/for.go
Normal file
35
go-by-example/for/for.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
|
||||
// Basic
|
||||
i := 1
|
||||
for i <= 3 {
|
||||
fmt.Println(i)
|
||||
i = i + 1
|
||||
}
|
||||
|
||||
// Classic initial/conditional/after loop
|
||||
for j := 0; j < 3; j++ {
|
||||
fmt.Println(j)
|
||||
}
|
||||
// or modernized
|
||||
for j := range 3 {
|
||||
fmt.Println("range", j)
|
||||
}
|
||||
// Will run until break
|
||||
for {
|
||||
fmt.Println("Break")
|
||||
break
|
||||
}
|
||||
// Continue
|
||||
for n := range 10 {
|
||||
if n%2 == 0 {
|
||||
continue
|
||||
}
|
||||
fmt.Println(n)
|
||||
}
|
||||
|
||||
}
|
||||
33
go-by-example/functions/functions.go
Normal file
33
go-by-example/functions/functions.go
Normal file
@@ -0,0 +1,33 @@
|
||||
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 vals() (int, int) {
|
||||
return 3, 7
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
res := plus(1, 2)
|
||||
fmt.Println("1 + 2 =", res)
|
||||
|
||||
res = plusPlus(1, 2, 3)
|
||||
fmt.Println("1 + 2 + 3 =", res)
|
||||
|
||||
a, b := vals()
|
||||
|
||||
fmt.Println(a)
|
||||
fmt.Println(b)
|
||||
|
||||
_, c := vals()
|
||||
fmt.Println(c)
|
||||
|
||||
}
|
||||
7
go-by-example/hello-world/hello-world.go
Normal file
7
go-by-example/hello-world/hello-world.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, World!")
|
||||
}
|
||||
42
go-by-example/maps/maps.go
Normal file
42
go-by-example/maps/maps.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
m := make(map[string]int)
|
||||
|
||||
m["k1"] = 7
|
||||
m["k2"] = 13
|
||||
|
||||
fmt.Println("map:", m)
|
||||
|
||||
v1 := m["k1"]
|
||||
fmt.Println("v1", v1)
|
||||
|
||||
v3 := m["k3"]
|
||||
fmt.Println("v3:", v3)
|
||||
|
||||
fmt.Println("len:", len(m))
|
||||
|
||||
delete(m, "k2")
|
||||
fmt.Println("map:", m)
|
||||
|
||||
clear(m)
|
||||
fmt.Println("map:", m)
|
||||
|
||||
_, prs := m["k2"]
|
||||
fmt.Println("prs:", prs)
|
||||
|
||||
n := map[string]int{"foo": 1, "bar": 2}
|
||||
fmt.Println("map:", n)
|
||||
|
||||
n2 := map[string]int{"foo": 1, "bar": 2}
|
||||
|
||||
if maps.Equal(n, n2) {
|
||||
fmt.Println("n == n2")
|
||||
}
|
||||
}
|
||||
48
go-by-example/slices/slices.go
Normal file
48
go-by-example/slices/slices.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
// "slices"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
var s []string
|
||||
fmt.Println("uninit", s, s == nil, len(s) == 0)
|
||||
|
||||
s = make([]string, 3)
|
||||
fmt.Println("emp:", s, "len:", len(s), "cap:", cap(s))
|
||||
|
||||
s[0] = "a"
|
||||
s[1] = "b"
|
||||
s[2] = "c"
|
||||
|
||||
fmt.Println("set:", s)
|
||||
fmt.Println("get:", s[2])
|
||||
|
||||
s = append(s, "d")
|
||||
s = append(s, "e", "f")
|
||||
|
||||
fmt.Println("apd:", s)
|
||||
|
||||
f := make([]int, 3, 10)
|
||||
fmt.Println("f:", f, "len:", len(f), "cap:", cap(f))
|
||||
|
||||
c := make([]string, len(s))
|
||||
copy(c, s)
|
||||
fmt.Println("cpy:", c)
|
||||
|
||||
l := s[2:5]
|
||||
fmt.Println("sl1:", l)
|
||||
// two d slices
|
||||
twoD := make([][]int, 10)
|
||||
|
||||
for i := range len(twoD) {
|
||||
innerLen := i + 1
|
||||
twoD[i] = make([]int, innerLen)
|
||||
for j := range innerLen {
|
||||
twoD[i][j] = 1 + j
|
||||
}
|
||||
}
|
||||
fmt.Println("2d: ", twoD)
|
||||
}
|
||||
49
go-by-example/switch/switch.go
Normal file
49
go-by-example/switch/switch.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
i := 2
|
||||
fmt.Print("Write ", i, " as ")
|
||||
switch i {
|
||||
case 1:
|
||||
fmt.Println("one")
|
||||
case 2:
|
||||
fmt.Println("two")
|
||||
case 3:
|
||||
fmt.Println("three")
|
||||
}
|
||||
|
||||
switch time.Now().Weekday() {
|
||||
case time.Saturday, time.Sunday:
|
||||
fmt.Println("It's the weekend")
|
||||
default:
|
||||
fmt.Println("It's a weekday")
|
||||
}
|
||||
|
||||
t := time.Now()
|
||||
switch {
|
||||
case t.Hour() < 12:
|
||||
fmt.Println("It's before noon")
|
||||
default:
|
||||
fmt.Println("It's after noon")
|
||||
}
|
||||
|
||||
whatAmI := func(i interface{}) {
|
||||
switch t := i.(type) {
|
||||
case bool:
|
||||
fmt.Println("I'm a bool")
|
||||
case int:
|
||||
fmt.Println("I'm an int")
|
||||
default:
|
||||
fmt.Printf("Don't know type %T\n", t)
|
||||
}
|
||||
}
|
||||
whatAmI(true)
|
||||
whatAmI(1)
|
||||
whatAmI("hey")
|
||||
}
|
||||
17
go-by-example/values/values.go
Normal file
17
go-by-example/values/values.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
|
||||
fmt.Println("go" + "lang")
|
||||
|
||||
fmt.Println("1+1 =", 1+1)
|
||||
|
||||
fmt.Println("7.0/3.0 =", 7.0/3.0)
|
||||
|
||||
fmt.Println(true && false)
|
||||
fmt.Println(true || false)
|
||||
fmt.Println(!true)
|
||||
|
||||
}
|
||||
22
go-by-example/variables/variables.go
Normal file
22
go-by-example/variables/variables.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
|
||||
var a = "initial"
|
||||
fmt.Println(a)
|
||||
|
||||
var b, c int = 1, 2
|
||||
fmt.Println(b, c)
|
||||
|
||||
var d = true
|
||||
fmt.Println(d)
|
||||
|
||||
var e int
|
||||
fmt.Println(e)
|
||||
|
||||
f := "apple"
|
||||
fmt.Println(f)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user