Skip to content

Commit 63121cf

Browse files
committed
Learn Go in 12 Minutes
1 parent 25caaf8 commit 63121cf

File tree

9 files changed

+175
-0
lines changed

9 files changed

+175
-0
lines changed

go/01-learn-go/01-hello-world.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("Hello, World!")
7+
}

go/01-learn-go/02-variables.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
x := 5
7+
y := 7
8+
sum := x + y
9+
10+
fmt.Println(sum)
11+
}

go/01-learn-go/03-if.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
x := 5
7+
8+
if x > 6 {
9+
fmt.Println("More than 6")
10+
} else if x < 2 {
11+
fmt.Println("Less than 2")
12+
} else {
13+
fmt.Println("Something else")
14+
}
15+
}

go/01-learn-go/04-arrays.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// An array has a length and a member type
7+
var a [5]int
8+
fmt.Println(a)
9+
10+
// Use square brackets to index an array
11+
a[2] = 7
12+
fmt.Println(a)
13+
14+
// Initialise an array with curly brackets
15+
b := [5]int{5, 4, 3, 2, 1}
16+
fmt.Println(b)
17+
18+
// Remove the element count to create a slice
19+
s := []int{5, 4, 3, 2, 1}
20+
fmt.Println(s)
21+
22+
// Use append to add something new to the array
23+
s = append(s, 13)
24+
fmt.Println(s)
25+
}

go/01-learn-go/05-maps.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
vertices := make(map[string]int)
7+
8+
vertices["triangle"] = 3
9+
vertices["square"] = 4
10+
vertices["dodecagon"] = 12
11+
12+
fmt.Println(vertices)
13+
fmt.Println(vertices["triangle"])
14+
15+
delete(vertices, "square")
16+
17+
fmt.Println(vertices)
18+
}

go/01-learn-go/06-loops.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// For loop
7+
for i := 0; i < 5; i++ {
8+
fmt.Println(i)
9+
}
10+
11+
// While loop
12+
i := 0
13+
for i < 5 {
14+
fmt.Println(i)
15+
i++
16+
}
17+
18+
// For each loop
19+
arr := [3]string{"a", "b", "c"}
20+
21+
for index, value := range arr {
22+
fmt.Println("index:", index, "value:", value)
23+
}
24+
25+
m := make(map[string]string)
26+
m["a"] = "alpha"
27+
m["b"] = "beta"
28+
29+
for key, value := range m {
30+
fmt.Println("key:", key, "value:", value)
31+
}
32+
}

go/01-learn-go/07-functions.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"math"
7+
)
8+
9+
func main() {
10+
result := sum(2, 3)
11+
fmt.Println(result)
12+
13+
result2, err := sqrt(16)
14+
if err != nil {
15+
fmt.Println(err)
16+
} else {
17+
fmt.Println(result2)
18+
}
19+
}
20+
21+
func sum(x, y int) int {
22+
return x + y
23+
}
24+
25+
func sqrt(x float64) (float64, error) {
26+
if x < 0 {
27+
return 0, errors.New("Undefined for negative numbers")
28+
}
29+
30+
return math.Sqrt(x), nil
31+
}

go/01-learn-go/08-struct.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type person struct {
6+
name string
7+
age int
8+
}
9+
10+
func main() {
11+
p := person{name: "Jake", age: 23}
12+
fmt.Println(p)
13+
fmt.Println(p.age)
14+
}

go/01-learn-go/09-pointers.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
i := 7
7+
fmt.Println("Memory address:", &i)
8+
9+
inc1(i)
10+
fmt.Println("i not modified:", i)
11+
12+
inc2(&i)
13+
fmt.Println("i incremented successfully:", i)
14+
}
15+
16+
func inc1(x int) {
17+
x++
18+
}
19+
20+
func inc2(x *int) {
21+
*x++
22+
}

0 commit comments

Comments
 (0)