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
+39
View File
@@ -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)
}
+23
View File
@@ -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)
}
+72
View File
@@ -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)
}
+41
View File
@@ -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], " "))
}
}
+18
View File
@@ -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)
}
+21
View File
@@ -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)
}
}
+33
View File
@@ -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!!")
}
}
+27
View File
@@ -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)
}