Skip to content

Commit eb519fc

Browse files
author
Konsti-dev
committed
Basics done
1 parent 9fe7fff commit eb519fc

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

10_pointers/main.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
a := 5
7+
b := &a
8+
9+
fmt.Println(a, b)
10+
11+
fmt.Printf("%T\n", b)
12+
13+
// Use * to read val from address
14+
15+
fmt.Println(*b)
16+
fmt.Println(*&a)
17+
18+
// Change val with pointer
19+
20+
*b = 10
21+
fmt.Println(a)
22+
}

11_closures/main.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func adder() func(int) int {
6+
sum := 0
7+
return func(x int) int {
8+
sum += x
9+
return sum
10+
}
11+
}
12+
13+
func main() {
14+
sum := adder()
15+
for i := 0; i < 10; i++ {
16+
fmt.Println(sum(i))
17+
}
18+
}

12_structs/main.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
)
7+
8+
// Define person struct
9+
10+
type Person struct {
11+
firstName, lastName, city, gender string
12+
age int
13+
}
14+
15+
// Greeting method (value reciever)
16+
func (p Person) greet() string {
17+
return "Hell, my name is " + p.firstName + " " + p.lastName + " and I am " + strconv.Itoa(p.age)
18+
}
19+
20+
// hasBirthday method (pointer reciever)
21+
func (p *Person) hasBirthday() {
22+
p.age++
23+
}
24+
25+
// getMarried (pointer reciever)
26+
func (p *Person) getMarried(spouseLastName string) {
27+
if p.gender == "Männlich" {
28+
return
29+
} else {
30+
p.lastName = spouseLastName
31+
}
32+
}
33+
34+
func main() {
35+
// Init person using struct
36+
37+
/* person1 := Person{firstName: "Konstantin", lastName: "Komendantov", city: "Heilbronn", gender: "Männlich", age: 21} */
38+
person2 := Person{"Konstantin", "Komendantov", "Neckarsulm-Amorbach", "Männlich", 21}
39+
person3 := Person{"Susanne", "Schmidt", "Neckarsulm-Amorbach", "Weiblich", 21}
40+
41+
/* fmt.Println(person2)
42+
43+
fmt.Println(person2.firstName)
44+
person2.age++
45+
fmt.Println(person2) */
46+
47+
person2.hasBirthday()
48+
fmt.Println(person2.greet())
49+
person3.getMarried("Frank")
50+
fmt.Println(person3.greet())
51+
52+
}

13_interfaces/main.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
// Define interface
9+
type Shape interface {
10+
area() float64
11+
}
12+
13+
type Circle struct {
14+
x, y, radius float64
15+
}
16+
17+
type Rectangle struct {
18+
width, height float64
19+
}
20+
21+
func (c Circle) area() float64 {
22+
return math.Pi * c.radius * c.radius
23+
}
24+
25+
func (r Rectangle) area() float64 {
26+
return r.width * r.height
27+
}
28+
29+
func getArea(s Shape) float64 {
30+
return s.area()
31+
}
32+
33+
func main() {
34+
circle := Circle{x: 0, y: 0, radius: 5}
35+
rectangle := Rectangle{width: 10, height: 5}
36+
37+
fmt.Printf("Circle Area: %f\n", getArea(circle))
38+
fmt.Printf("Circle Area: %f\n", getArea(rectangle))
39+
}

14_web/main.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func index(w http.ResponseWriter, r *http.Request) {
9+
fmt.Fprintf(w, "<h1>Hello World</h1>")
10+
}
11+
12+
func about(w http.ResponseWriter, r *http.Request) {
13+
fmt.Fprintf(w, "<h1>Konsti-Dev</h1>")
14+
}
15+
16+
func main() {
17+
http.HandleFunc("/", index)
18+
http.HandleFunc("/about", about)
19+
fmt.Println("server Starting...")
20+
http.ListenAndServe(":3000", nil)
21+
}

0 commit comments

Comments
 (0)