From 1d3fdf9c4d330379701539bd7b71bd1d31942b66 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 3 Jul 2021 14:02:17 +0300 Subject: Finished Go tutorial --- Go/tutorial.go | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 211 insertions(+), 2 deletions(-) diff --git a/Go/tutorial.go b/Go/tutorial.go index 8230d13..353fc82 100644 --- a/Go/tutorial.go +++ b/Go/tutorial.go @@ -4,14 +4,202 @@ package main import ( "bufio" "fmt" + "math" "os" "strconv" ) func main() { - arrays() + // variablesAndPrinting() + // input() + // conditionals() + // loops() + // arrays() + // functions() + // pointers() + // structs() + interfaces() } +/* Intefaces */ + +type shape interface { + area() float64 +} + +type circle struct { + radius float64 +} + +type rect struct { + width float64 + height float64 +} + +func (c circle) area() float64 { + return math.Pi * c.radius * c.radius +} + +func (r rect) area() float64 { + return r.width * r.height +} + +func interfaces() { + var sh shape = circle{5} + fmt.Println(sh.area()) + sh = rect{3, 8} + fmt.Println(sh.area()) +} + +/* Structs */ + +type Point struct { + x int + y int +} + +type Circle struct { + radius float64 + *Point // struct composition +} + +type Person struct { + name string + age int +} + +func (p Person) getAge() int { + return p.age +} + +func (p Person) setAgeBroken(newAge int) { + p.age = newAge +} + +func (p *Person) setAgeWorking(newAge int) { + p.age = newAge +} + +func structs() { + var p1 Point = Point{1, 2} + p1.x = 5 + fmt.Println(p1.x, p1.y) + + p2 := Point{ + x: 19, + } + fmt.Println(p2.x, p2.y) // p2.y is type default + + c1 := Circle{3.14, &Point{30, 40}} + fmt.Println(c1, c1.x, c1.y) + + person1 := Person{"John", 29} + fmt.Println(person1.getAge()) + person1.setAgeBroken(19) + fmt.Println(person1.age) + person1.setAgeWorking(19) + fmt.Println(person1.age) +} + +/* Pointers */ + +func pointers() { + x := 7 + fmt.Println(&x) + + var y *int = &x + fmt.Println(x, y) + *y = 10 + fmt.Println(x, y) + + changeValue := func(str *string) { + *str = "Changed value!" + } + str := "Test" + changeValue(&str) + fmt.Println(str) +} + +/* Functions */ + +func print(x string) { + fmt.Println(x) +} + +func printMultiple(msg1 string, msg2 string) { + fmt.Println(msg1, msg2) +} + +func printMultiple1(msg1, msg2 string, n1 int) { + fmt.Println(msg1, msg2, 5) +} + +func sum(a, b int) int { + return a + b +} + +func sumAndSub(a, b int) (int, int) { + return a + b, a - b +} + +func sumAndSubAlt(a, b int) (z1 int, z2 int) { + // Can be simplified just to: z1, z2 = a+b, a-b + z1 = a + b + z2 = a - b + return +} + +func sumAndSubAlt1(a, b int) (z1 int, z2 int) { + defer fmt.Println("function ended") // defer statements execute when a function finishes + z1, z2 = a+b, a-b + return +} + +func functions() { + print("Hello World") + printMultiple("One", "Two") + printMultiple1("One", "Two", 5) + fmt.Println(sum(1, 2)) + fmt.Println(sumAndSub(1, 2)) + fmt.Println(sumAndSubAlt(1, 2)) + fmt.Println(sumAndSubAlt1(1, 2)) + + test := func() { + fmt.Println("test variable") + } + test() + + // Saves a function and execution of it, so it saves what the function returns if given + // the number 9 + test1 := func(x int) int { + return x * 2 + }(9) + fmt.Println(test1) + + test3 := func(f func()) { + f() + } + test3(test) + + func() { + fmt.Println("woah") + }() + + returnFunc := func(id int) func(int) int { + switch id { + case 1: + return func(n int) int { return n * 2 } + case 2: + return func(n int) int { return n * 3 } + default: + return func(n int) int { return n * -1 } + } + } + fmt.Println(returnFunc(2)(5)) +} + +/* Arrays */ + func arrays() { var arr [5]int // fills with default value arr[2] = 5 @@ -45,8 +233,23 @@ func arrays() { for _, elem := range bigarr { // equivalent to this C# code: foreach(var elem in bigarr) fmt.Println(elem) } + + // Maps are the Go equivalent of C# Dictionaries + mp := map[string]int{ + "apple": 1, + "pear": 2, + "orange": 9, + } + fmt.Println(mp) + fmt.Println(mp["orange"]) + delete(mp, "orange") + fmt.Println(mp) + val, exists := mp["adf"] // If value exists at key, val is assigned to it and exists is true, otherwise val is default for type and exists is false + fmt.Println(exists, val) } +/* Loops */ + func loops() { i = 0 for i < 5 { @@ -68,6 +271,8 @@ func loops() { } } +/* Conditionals */ + func conditionals() { age := 21 if driving_age := 18; age < driving_age { @@ -96,6 +301,8 @@ func conditionals() { } } +/* User input */ + func input() { scanner := bufio.NewScanner(os.Stdin) @@ -112,7 +319,9 @@ func input() { } -var i int +/* Variables and printing */ + +var i int // global variable func variablesAndPrinting() { var msg string -- cgit v1.2.3