├── .gitignore ├── README.md ├── closures └── main.go ├── conditionals ├── else.go ├── else_if.go ├── if.go └── switch.go ├── datatypes ├── _integer.go ├── show_types.go ├── strings.go └── types_conversion.go ├── functions ├── alias.go ├── functions.go ├── paremeters.go └── return.go ├── go.mod ├── helloworld └── main.go ├── interfaces └── main.go ├── lists ├── arrays.go ├── maps.go ├── muldimensional_array.go ├── range.go └── slices.go ├── loops ├── fizbuzz.go ├── for.go └── while.go ├── main.go ├── operators ├── assingment_operators.go ├── logical_operators.go ├── numerical_operators.go └── relational_operators.go ├── packages ├── greet │ └── greet.go ├── hello │ └── hello.go ├── main.go └── mystrutil │ └── reverse.go ├── pointers └── pointers.go ├── range └── range.go ├── simplePackage └── simplePackage.go ├── std ├── fmt.go ├── mathPackage.go ├── os │ ├── read_arguments.go │ └── read_file.go └── userInput.go ├── structs └── main.go ├── testing └── calc │ ├── operations.go │ └── operations_test.go └── variables ├── constants.go ├── declare_variables.go ├── outside_variables.go ├── reasign_variable.go ├── reusability.go ├── shadowing.go ├── shorthand_declaration.go ├── static_typing.go └── type_inference.go /.gitignore: -------------------------------------------------------------------------------- 1 | bin/ 2 | 3 | test.txt -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Go Course 2 | 3 | This is a basic course of Go programming language designed to introduce all the basic concepts of the language -------------------------------------------------------------------------------- /closures/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func adder() func(int) int { 8 | sum := 0 9 | return func(x int) int { 10 | sum += x 11 | return sum 12 | } 13 | } 14 | 15 | func main() { 16 | 17 | sum := adder() 18 | 19 | for i := 0; i < 10; i++ { 20 | fmt.Println(sum(i)) 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /conditionals/else.go: -------------------------------------------------------------------------------- 1 | package conditionals 2 | 3 | import "fmt" 4 | 5 | func Else() { 6 | x := 10 7 | y := 10 8 | 9 | if x <= y { 10 | fmt.Printf("%d is less than or equal %d\n", x, y) 11 | } else { 12 | fmt.Printf("%d is less than %d\n", y, x) 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /conditionals/else_if.go: -------------------------------------------------------------------------------- 1 | package conditionals 2 | 3 | import "fmt" 4 | 5 | func ElseIf() { 6 | 7 | // else if 8 | color := "orange" 9 | 10 | if color == "red" { 11 | fmt.Println("color is red") 12 | } else if color == "blue" { 13 | fmt.Println("color is blue") 14 | } else if color == "yellow" { 15 | fmt.Println("color is yellow") 16 | } else { 17 | fmt.Println("I don't know this color") 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /conditionals/if.go: -------------------------------------------------------------------------------- 1 | package conditionals 2 | 3 | import "fmt" 4 | 5 | func If() { 6 | busy := true 7 | 8 | if busy { 9 | fmt.Println("I am sad \u2639") 10 | } 11 | 12 | if !busy { 13 | fmt.Println("I am not married \u263A") 14 | } 15 | 16 | status := "slpeeping" 17 | 18 | if status == "working" { 19 | fmt.Println("Iam working") 20 | } 21 | 22 | if status == "free" { 23 | fmt.Println("talk to me") 24 | } 25 | 26 | if status == "sleeping" { 27 | fmt.Println("Do not disturb") 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /conditionals/switch.go: -------------------------------------------------------------------------------- 1 | package conditionals 2 | 3 | import "fmt" 4 | 5 | func Switch() { 6 | 7 | color := "orange" 8 | 9 | // Switch 10 | switch color { 11 | case "red": 12 | fmt.Println("color is red") 13 | case "blue": 14 | fmt.Println("color is blue") 15 | case "yellow": 16 | fmt.Println("color is yellow") 17 | default: 18 | fmt.Println("I don't know this color") 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /datatypes/_integer.go: -------------------------------------------------------------------------------- 1 | // uint uint8 uint16 uint32 uint64 uintptr 2 | // byte - alias for uint8 3 | // rune - alias for uint32 4 | // https://golang.org/pkg/builtin/#int16 5 | 6 | 7 | // complex64 complex128 8 | 9 | // Derivated Types 10 | -------------------------------------------------------------------------------- /datatypes/show_types.go: -------------------------------------------------------------------------------- 1 | package datatypes 2 | 3 | import "fmt" 4 | 5 | func ShowTypes() { 6 | // Literals 7 | fmt.Println("Hello World") // string 8 | fmt.Println(true) // boolean 9 | fmt.Println(33) // integer 10 | fmt.Println(100.44) // float 11 | 12 | fmt.Println() 13 | 14 | // data types 15 | fmt.Printf("%T\n", "Hello World") 16 | fmt.Printf("%T\n", true) 17 | fmt.Printf("%T\n", 33) 18 | fmt.Printf("%T\n", 100.44) 19 | } 20 | -------------------------------------------------------------------------------- /datatypes/strings.go: -------------------------------------------------------------------------------- 1 | package datatypes 2 | 3 | import "fmt" 4 | 5 | func Strings() { 6 | 7 | // a simple string in go 8 | fmt.Println("Hello World") 9 | 10 | // we can store in variables 11 | var courseName string = "Go Course" 12 | fmt.Println(courseName) 13 | 14 | // you can see the type of variable with %T 15 | fmt.Printf("%T\n", courseName) 16 | 17 | // in go we can use the backtick ` to create multiline strings 18 | var multiline string = ` 19 | Hello World 20 | from Go Course 21 | ` 22 | fmt.Println(multiline) 23 | 24 | // length of string 25 | fmt.Println(len(courseName)) 26 | 27 | // access to a character 28 | fmt.Println(courseName[0]) // 71, the ascii code of G 29 | fmt.Println(string(courseName[0])) // G 30 | 31 | fmt.Println(courseName[0:2]) // Go 32 | fmt.Println(courseName[:2]) // Go 33 | fmt.Println(courseName[3:]) // Course 34 | fmt.Println(courseName[:]) // Go Course 35 | 36 | // we can use the + operator to concatenate strings 37 | fmt.Println("Hello " + "World") // Hello World 38 | fmt.Println("Hello " + courseName) // Hello Go Course 39 | 40 | // we can use the += operator to concatenate strings 41 | var hello string = "Hello " 42 | hello += "World" 43 | hello += " !" 44 | hello += " from Go Course" 45 | fmt.Println(hello) // Hello World ! from Go Course 46 | 47 | // interpolation 48 | fmt.Printf("Welcome to %s!\n", courseName) // Welcome to Go Course! 49 | 50 | // interpolation with numbers 51 | fmt.Printf("The number is %d\n", 10) // The number is 10 52 | 53 | // interpolation with floats 54 | fmt.Printf("The float is %f\n", 10.5) // The float is 10.500000 55 | 56 | // interpolation with booleans 57 | fmt.Printf("The boolean is %t\n", true) // The boolean is true 58 | 59 | // interpolation with characters 60 | fmt.Printf("The character is %c\n", 71) // The character is G 61 | 62 | // interpolate multiple values 63 | var age = 30 64 | var name = "John" 65 | var married = false 66 | 67 | fmt.Printf("%s is %d years old and it is %t that he is married\n", name, age, married) 68 | 69 | // utf-8 70 | fmt.Println("Hello \xF0\x9F\x98\x8E") 71 | fmt.Println("Hello \U0001F60E") 72 | fmt.Println("Hello \U0001F915") 73 | fmt.Println("Hello 😴") 74 | 75 | } 76 | -------------------------------------------------------------------------------- /datatypes/types_conversion.go: -------------------------------------------------------------------------------- 1 | package datatypes 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | "strconv" 7 | ) 8 | 9 | func TypeConversion() { 10 | var a int = 42 11 | fmt.Printf("%v %T\n", a, a) // 42 int 12 | 13 | // use the float32 function to convert 14 | var b float32 15 | b = float32(a) 16 | fmt.Printf("%v %T\n", b, b) // 42 float32 17 | 18 | // but if convert from float to int, you can lose data 19 | var f float32 = 455.78 20 | fmt.Printf("%v %T\n", f, f) 21 | 22 | var g int = int(f) 23 | fmt.Printf("%v %T\n", g, g) 24 | 25 | // convert int to string 26 | var h int = 70 27 | fmt.Printf("%v %T\n", h, h) 28 | 29 | // this convert to the unicode equivalent 30 | var i string 31 | i = string(h) 32 | fmt.Printf("%v %T\n", i, i) // F string 33 | 34 | // using the strconv package 35 | var j int = 100 36 | // var k string = string(j) 37 | var k string = strconv.Itoa(j) 38 | fmt.Printf("%v %T\n", k, k) // 100 string 39 | 40 | var l = "10033" 41 | var m, err = strconv.Atoi(l) 42 | fmt.Println(m, err, reflect.TypeOf(m)) // 10033 int 43 | 44 | } 45 | -------------------------------------------------------------------------------- /functions/alias.go: -------------------------------------------------------------------------------- 1 | package functions 2 | 3 | import "fmt" 4 | 5 | var p = fmt.Println 6 | 7 | func Alias() { 8 | p("Hello World") 9 | p("Hello World 2") 10 | p("Hello Go!") 11 | } 12 | -------------------------------------------------------------------------------- /functions/functions.go: -------------------------------------------------------------------------------- 1 | package functions 2 | 3 | import "fmt" 4 | 5 | func hello() { 6 | fmt.Println("Hello") 7 | } 8 | 9 | // func operateTotal(total int) (x int, y int) { 10 | // x = total + 20 11 | // y = total - 50 12 | // return 13 | // } 14 | 15 | func Functions() { 16 | // First function 17 | hello() 18 | hello() 19 | hello() 20 | hello() 21 | hello() 22 | 23 | // named return 24 | fmt.Println(operateTotal(10)) 25 | } 26 | -------------------------------------------------------------------------------- /functions/paremeters.go: -------------------------------------------------------------------------------- 1 | package functions 2 | 3 | import "fmt" 4 | 5 | func sum(x int, y int) { 6 | fmt.Println(x + y) 7 | } 8 | 9 | func sum2(x, y int) { 10 | fmt.Println(x + y) 11 | } 12 | 13 | func Parameters() { 14 | sum(10, 20) 15 | sum2(40, 70) 16 | } 17 | -------------------------------------------------------------------------------- /functions/return.go: -------------------------------------------------------------------------------- 1 | package functions 2 | 3 | import "fmt" 4 | 5 | func helloWorld() string { 6 | return "Hello World" 7 | } 8 | 9 | // return int 10 | func add(num1 int, num2 int) int { 11 | return num1 + num2 12 | } 13 | 14 | // return string with parameter 15 | func greeting(name string) string { 16 | return "Hello " + name 17 | } 18 | 19 | // return multiple parameters 20 | func swap(a, b string) (string, string) { 21 | return b, a 22 | } 23 | 24 | func operateTotal(n int) (x int, y int){ 25 | x = n + 100 26 | y = n + 500 27 | return 28 | } 29 | 30 | func Return() { 31 | fmt.Println(helloWorld()) 32 | fmt.Println(greeting("fazt")) 33 | fmt.Println(add(10, 15)) 34 | 35 | fmt.Println(swap("hello", "world")) 36 | 37 | lastname, name := swap("Joe", "Harper") 38 | fmt.Println(lastname, name) 39 | 40 | // named return 41 | fmt.Println(operateTotal(10)) 42 | } 43 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/fazttech/go-course 2 | 3 | go 1.18 4 | -------------------------------------------------------------------------------- /helloworld/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | fmt.Println("Hello World") 9 | } 10 | -------------------------------------------------------------------------------- /interfaces/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | ) 7 | 8 | type Shape interface { 9 | area() float64 10 | } 11 | 12 | type Circle struct { 13 | x, y, radius float64 14 | } 15 | 16 | type Rectangle struct { 17 | width, height float64 18 | } 19 | 20 | func (c Circle) area() float64 { 21 | return math.Pi * c.radius * c.radius 22 | } 23 | 24 | func (r Rectangle) area() float64 { 25 | return r.width * r.height 26 | } 27 | 28 | func getArea(s Shape) float64 { 29 | return s.area() 30 | } 31 | 32 | func main() { 33 | circle := Circle{x: 0, y: 0, radius: 5} 34 | rectangle := Rectangle{width: 10, height: 5} 35 | fmt.Printf("Circle Area: %f\n", getArea(circle)) 36 | fmt.Printf("Circle Area: %f\n", getArea(rectangle)) 37 | } 38 | -------------------------------------------------------------------------------- /lists/arrays.go: -------------------------------------------------------------------------------- 1 | package lists 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func Arrays() { 8 | // Array 9 | var users [2]string 10 | fmt.Println(users) 11 | 12 | // assign values 13 | var favoriteFruits [3]string 14 | favoriteFruits[0] = "apple" 15 | favoriteFruits[1] = "orange" 16 | favoriteFruits[2] = "pineapple" 17 | fmt.Println(favoriteFruits) 18 | 19 | // Declare and assign 20 | programmingLanguages := [3]string{"Javascript", "Go", "Python"} 21 | fmt.Println(programmingLanguages) 22 | 23 | // length 24 | fmt.Println(len(programmingLanguages)) 25 | 26 | // short declaration 27 | friends := [3]string{"fazt", "jesus", "joe"} 28 | fmt.Println(friends) 29 | 30 | // ellipsis 31 | brothers := [...]string{"fazt", "jesus", "joe", "jose"} 32 | fmt.Println(brothers) 33 | 34 | // access values 35 | cities := [3]string{"New York", "London", "Tokyo"} 36 | fmt.Println(cities[0]) 37 | fmt.Println(cities[1]) 38 | fmt.Println(cities[2]) 39 | 40 | // range 41 | notes := [...]string{"do", "re", "mi", "fa", "sol", "la", "si"} 42 | fmt.Println(notes[2:4]) 43 | } 44 | -------------------------------------------------------------------------------- /lists/maps.go: -------------------------------------------------------------------------------- 1 | package lists 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func Maps() { 8 | // define a map 9 | students := make(map[string]string) 10 | 11 | // assign values 12 | students["fazt"] = "fazt@faztweb.com" 13 | students["jesus"] = "jesus@faztweb.com" 14 | students["joe"] = "joe@faztweb.com" 15 | fmt.Println(students) 16 | 17 | scores := make(map[string]int) 18 | 19 | scores["fazt"] = 100 20 | scores["jesus"] = 90 21 | scores["joe"] = 80 22 | fmt.Println(scores) 23 | 24 | // len 25 | fmt.Println(len(students)) 26 | 27 | // access values 28 | fmt.Println(scores["fazt"]) 29 | 30 | // delete from map 31 | delete(students, "jesus") 32 | fmt.Println(students) 33 | 34 | // declare and assign values 35 | users := map[string]int{"fazt": 50, "jesus": 100, "joe": 70} 36 | 37 | fmt.Println(users) 38 | 39 | // check if key exists 40 | _, ok := users["fazt"] 41 | 42 | if ok { 43 | fmt.Println("user exists") 44 | } else { 45 | fmt.Println("user doesn't exists") 46 | } 47 | 48 | // check if key exists 49 | if _, ok := users["jose"]; ok { 50 | fmt.Println("Key exists") 51 | } else { 52 | fmt.Println("Key doesn't exists") 53 | } 54 | 55 | // check if key does not exists 56 | if _, ok := users["jose"]; !ok { 57 | fmt.Println("Key doesn't exists") 58 | } else { 59 | fmt.Println("Key exists") 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /lists/muldimensional_array.go: -------------------------------------------------------------------------------- 1 | package lists 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | // multidimensional array 8 | var matrix [2][2]int 9 | 10 | matrix[0][0] = 1 11 | matrix[0][1] = 2 12 | 13 | matrix[1][0] = 3 14 | matrix[1][1] = 4 15 | 16 | fmt.Println(matrix) 17 | 18 | // declare and assign 19 | matrix2 := [2][2]int{{1, 2}, {3, 4}} 20 | fmt.Println(matrix2) 21 | 22 | // short declaration 23 | matrix3 := [...][2]int{{1, 2}, {3, 4}} 24 | 25 | fmt.Println(matrix3) 26 | 27 | // range 28 | for i := 0; i < len(matrix3); i++ { 29 | for j := 0; j < len(matrix3[i]); j++ { 30 | fmt.Println(matrix3[i][j]) 31 | } 32 | } 33 | 34 | // range 35 | for _, row := range matrix3 { 36 | for _, value := range row { 37 | fmt.Println(value) 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /lists/range.go: -------------------------------------------------------------------------------- 1 | package lists 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func Range() { 8 | 9 | ids := []int{50, 100, 77, 10} 10 | for i, id := range ids { 11 | fmt.Printf("id %d: %d\n", i, id) 12 | } 13 | 14 | // range a slice without index 15 | points := []int{15, 20, 25, 30} 16 | 17 | for _, point := range points { 18 | fmt.Println(point) 19 | } 20 | 21 | // range and calculate sum 22 | expenses := []int{14, 20, 100} 23 | sum := 0 24 | for _, num := range expenses { 25 | sum += num 26 | } 27 | fmt.Println("total expenses:", sum) 28 | 29 | // range with condition 30 | ages := []int{35, 20, 15, 30} 31 | for i, age := range ages { 32 | if age <= 15 { 33 | fmt.Println(i, ": The age", age, "is not allowed") 34 | } 35 | fmt.Println(i, ": The age", age, "is allowed") 36 | } 37 | 38 | // range a map 39 | kvs := map[string]string{"a": "apple", "b": "banana"} 40 | for k, v := range kvs { 41 | fmt.Printf("%s -> %s", k, v) 42 | } 43 | 44 | for k := range kvs { 45 | fmt.Println("key:", k) 46 | } 47 | // range 48 | // for key, value := range students{ 49 | // fmt.Println(key, value) 50 | // } 51 | 52 | // range a string 53 | for i, c := range "go" { 54 | // fmt.Println(i, c) 55 | fmt.Println(i, string(c)) 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /lists/slices.go: -------------------------------------------------------------------------------- 1 | package lists 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func Slices() { 8 | // Slice 9 | tasksSlice := []string{"I have to run", "Buy milk", "walking the dog", "I have to sleep"} 10 | 11 | // length 12 | fmt.Println(len(tasksSlice)) 13 | fmt.Println(tasksSlice[0]) 14 | fmt.Println(tasksSlice[1]) 15 | fmt.Println(tasksSlice[2]) 16 | 17 | // range 18 | fmt.Println(tasksSlice[1:3]) 19 | 20 | // append 21 | tasksSlice = append(tasksSlice, "I have to eat") 22 | fmt.Println(tasksSlice) 23 | 24 | // delete the index 1 25 | tasksSlice = append(tasksSlice[:1], tasksSlice[2:]...) 26 | fmt.Println(tasksSlice) 27 | 28 | // make 29 | tasksSlice2 := make([]string, 3) 30 | 31 | tasksSlice2[0] = "I have to run" 32 | tasksSlice2[1] = "Buy milk" 33 | tasksSlice2[2] = "walking the dog" 34 | tasksSlice2 = append(tasksSlice2, "I have to sleep") 35 | 36 | fmt.Println(tasksSlice2) 37 | } 38 | -------------------------------------------------------------------------------- /loops/fizbuzz.go: -------------------------------------------------------------------------------- 1 | package loops 2 | 3 | import "fmt" 4 | 5 | func Fizbuzz() { 6 | 7 | for i := 1; i <= 100; i++ { 8 | if i%15 == 0 { 9 | fmt.Println("FizzBuzz") 10 | } else if i%3 == 0 { 11 | fmt.Println("Fizz") 12 | } else if i%5 == 0 { 13 | fmt.Println("Buzz") 14 | } else { 15 | fmt.Println(i) 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /loops/for.go: -------------------------------------------------------------------------------- 1 | package loops 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func For() { 8 | // short method 9 | 10 | for i := 1; i <= 10; i++ { 11 | fmt.Printf("Number %d\n", i) 12 | } 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /loops/while.go: -------------------------------------------------------------------------------- 1 | package loops 2 | 3 | import "fmt" 4 | 5 | func While() { 6 | // for loop as while 7 | i := 1 8 | 9 | // long method 10 | for i <= 10 { 11 | fmt.Println(i) 12 | // i = i + 1 13 | i++ 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import std "github.com/fazttech/go-course/std/os" 4 | 5 | // go packages 6 | func main() { 7 | std.ReadFiles() 8 | } 9 | -------------------------------------------------------------------------------- /operators/assingment_operators.go: -------------------------------------------------------------------------------- 1 | package operators 2 | 3 | import "fmt" 4 | 5 | // AssignmentOperators ... 6 | func AssignmentOperators() { 7 | var x = 30 8 | 9 | x = x + 10 10 | fmt.Println(x) 11 | 12 | x += 10 13 | fmt.Println(x) 14 | 15 | x -= 10 16 | fmt.Println(x) 17 | 18 | x *= 10 19 | fmt.Println(x) 20 | 21 | x /= 10 22 | fmt.Println(x) 23 | 24 | x %= 10 25 | fmt.Println(x) 26 | } 27 | -------------------------------------------------------------------------------- /operators/logical_operators.go: -------------------------------------------------------------------------------- 1 | package operators 2 | 3 | import "fmt" 4 | 5 | func LogicalOperator() { 6 | // logical operators 7 | fmt.Println(true && true) 8 | fmt.Println(true && false) 9 | fmt.Println(true || true) 10 | fmt.Println(true || false) 11 | fmt.Println(!true) 12 | 13 | // logical operators with relational operators 14 | fmt.Println(10 == 10 && 10 > 5) // true 15 | fmt.Println(10 == 10 || 10 > 11) // true 16 | } 17 | -------------------------------------------------------------------------------- /operators/numerical_operators.go: -------------------------------------------------------------------------------- 1 | package operators 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func NumericalOperator() { 8 | fmt.Println(10 + 10) 9 | fmt.Println(10 - 7) 10 | fmt.Println(10 * 2) 11 | fmt.Println(10 / 5) 12 | fmt.Println(10 % 7) 13 | // fmt.Println(math.Pow(2, 3)) // this is not an operator, but a function 14 | } 15 | -------------------------------------------------------------------------------- /operators/relational_operators.go: -------------------------------------------------------------------------------- 1 | package operators 2 | 3 | import "fmt" 4 | 5 | func RelationalOperator() { 6 | fmt.Println(10 == 11) 7 | fmt.Println(10 != 9) 8 | fmt.Println(10 > 11) 9 | fmt.Println(10 < 20) 10 | fmt.Println(10 >= 30) 11 | fmt.Println(10 <= 20) 12 | } 13 | -------------------------------------------------------------------------------- /packages/greet/greet.go: -------------------------------------------------------------------------------- 1 | package greet 2 | 3 | func Greet(name string) string { 4 | return "Hello " + name + "!" 5 | } 6 | -------------------------------------------------------------------------------- /packages/hello/hello.go: -------------------------------------------------------------------------------- 1 | package hello 2 | 3 | import "fmt" 4 | 5 | func Greet() { 6 | fmt.Println("Hello") 7 | } 8 | -------------------------------------------------------------------------------- /packages/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/fazttech/go-course/packages/greet" 7 | ) 8 | 9 | func main() { 10 | fmt.Println(greet.Greet("Fazt")) 11 | } 12 | -------------------------------------------------------------------------------- /packages/mystrutil/reverse.go: -------------------------------------------------------------------------------- 1 | package mystrutil 2 | 3 | func Reverse(s string) string { 4 | runes := []rune(s) 5 | for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { 6 | runes[i], runes[j] = runes[j], runes[i] 7 | } 8 | return string(runes) 9 | } 10 | -------------------------------------------------------------------------------- /pointers/pointers.go: -------------------------------------------------------------------------------- 1 | package pointers 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func Pointers() { 8 | a := 5 // value 9 | b := &a // pointer to a 10 | 11 | fmt.Println(a, b) // 5 0xc0000b4008 12 | 13 | // read values from addresses 14 | fmt.Println(*b) // 5 15 | fmt.Println(*&a) // 5 16 | 17 | // change value with pointers 18 | *b = 10 19 | 20 | fmt.Println(a) 21 | } 22 | -------------------------------------------------------------------------------- /range/range.go: -------------------------------------------------------------------------------- 1 | package rangePackage 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func RangeIntro() { 8 | ids := []int{50, 100, 77, 10} 9 | 10 | // loop 11 | for i, id := range ids { 12 | fmt.Printf("id %d: %d\n", i, id) 13 | } 14 | 15 | // without using index 16 | for _, id := range ids { 17 | fmt.Printf("ID: %d\n", id) 18 | } 19 | 20 | // sum ids 21 | sum := 0 22 | for _, id := range ids { 23 | sum += id 24 | } 25 | 26 | println(sum) 27 | 28 | // range with maps 29 | emails := map[string]string{"fazt": "fazt@faztweb.com", "jesus": "jesus@gmail.com", "ryan": "ryan@gmail.com"} 30 | 31 | // loop the keys 32 | for k := range emails { 33 | fmt.Println("Name:" + k) 34 | } 35 | 36 | // loop the values 37 | for _, v := range emails { 38 | fmt.Println("email:" + v) 39 | } 40 | 41 | // loop the keys and values 42 | for k, v := range emails { 43 | fmt.Printf("%s: \t\t%s\n", k, v) 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /simplePackage/simplePackage.go: -------------------------------------------------------------------------------- 1 | package simplePackage 2 | 3 | func AddTwo(x, y int) int { 4 | return x + y 5 | } 6 | -------------------------------------------------------------------------------- /std/fmt.go: -------------------------------------------------------------------------------- 1 | package std 2 | 3 | import "fmt" 4 | 5 | func Fmt() { 6 | name := "Joe" 7 | fmt.Println("Hello", name, "Welcome") 8 | 9 | nickname := "Fazt" 10 | fmt.Printf("Hello %s, Welcome to my program!\n", nickname) 11 | 12 | // print format data with Struct 13 | type CategoryProduct struct { 14 | name string 15 | price float32 16 | } 17 | 18 | laptop := CategoryProduct{ 19 | name: "Macbook Pro", 20 | price: 1299.99, 21 | } 22 | 23 | fmt.Printf("%v\n", laptop) 24 | fmt.Printf("%+v\n", laptop) 25 | 26 | // Print datatypes of variables 27 | username := "Fazt" 28 | age := 70 29 | isAlive := true 30 | score := 152.30 31 | 32 | // print the datatypes of variables 33 | fmt.Printf("%T\n", username) 34 | fmt.Printf("%T\n", age) 35 | fmt.Printf("%T\n", isAlive) 36 | fmt.Printf("%T\n", score) 37 | 38 | // print the value and its datatypes 39 | fmt.Printf("%v %T\n", username, username) 40 | 41 | } 42 | -------------------------------------------------------------------------------- /std/mathPackage.go: -------------------------------------------------------------------------------- 1 | package std 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | "math/rand" 7 | "time" 8 | ) 9 | 10 | func MathPackage() { 11 | rand.Seed(time.Now().UnixNano()) 12 | fmt.Println(rand.Intn(10)) // random number between 0 and 10 13 | fmt.Println(rand.Float64()) // random float64 between 0 and 1 14 | fmt.Println(rand.Float64() * 5) // random float64 between 0 and 5 15 | 16 | // asbsolute value 17 | fmt.Println(math.Abs(-1)) // 1 18 | } 19 | -------------------------------------------------------------------------------- /std/os/read_arguments.go: -------------------------------------------------------------------------------- 1 | package std 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func OsArguments() { 9 | 10 | fmt.Println(os.Args) 11 | 12 | if len(os.Args) != 2 { 13 | fmt.Println("Please provide a name") 14 | return 15 | } 16 | 17 | argument := os.Args[1] 18 | 19 | fmt.Println("Hello", argument) 20 | 21 | fmt.Println(os.Args[0]) 22 | fmt.Println(os.Args[1]) 23 | } 24 | -------------------------------------------------------------------------------- /std/os/read_file.go: -------------------------------------------------------------------------------- 1 | package std 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "os" 7 | ) 8 | 9 | func ReadFiles() { 10 | // this line, open the file and return a file object, if the file doesn't exist, create it 11 | // and put the file object in the variable file 12 | // the second parameter is the flag, if the file doesn't exist, create it 13 | // the third parameter is the permission 14 | file, err := os.OpenFile("test.txt", os.O_RDWR|os.O_CREATE, 0755) 15 | 16 | if err != nil { 17 | fmt.Println(err) 18 | return 19 | } 20 | 21 | defer file.Close() 22 | 23 | scanner := bufio.NewScanner(file) 24 | for scanner.Scan() { 25 | fmt.Println(scanner.Text()) 26 | } 27 | 28 | if err := scanner.Err(); err != nil { 29 | fmt.Println(err) 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /std/userInput.go: -------------------------------------------------------------------------------- 1 | package std 2 | 3 | import "fmt" 4 | 5 | func userInput() { 6 | var name string 7 | fmt.Print("What is your name?: ") 8 | fmt.Scan(&name) 9 | fmt.Println(name) 10 | } 11 | -------------------------------------------------------------------------------- /structs/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // defint a person struct 9 | type Person struct { 10 | // firstName string 11 | // lastName string 12 | // gender string 13 | 14 | firstName, lastName, gender string 15 | age int 16 | } 17 | 18 | // Greeting method (value reciever) 19 | func (p Person) greet() string { 20 | return "Hello, my name is " + p.firstName + " " + p.lastName + " and I have: " + strconv.Itoa(p.age) 21 | } 22 | 23 | // hasBirthday method (pointer receiver) 24 | func (p *Person) hasBirthday() { 25 | p.age++ 26 | } 27 | 28 | // getMarried (value receiver) 29 | func (p *Person) getMarried(spouseLastName string) { 30 | if p.gender == "m" { 31 | return 32 | } else { 33 | p.lastName = spouseLastName 34 | } 35 | } 36 | 37 | func main() { 38 | 39 | // init person using struct 40 | person1 := Person{firstName: "jesus", lastName: "McMillan", age: 30, gender: "m"} 41 | 42 | // second person 43 | person2 := Person{"Jane", "McMillan", "f", 40} 44 | 45 | fmt.Println(person1) 46 | fmt.Println(person2) 47 | 48 | fmt.Println(person1.firstName) 49 | 50 | person1.age++ 51 | fmt.Println(person1) 52 | 53 | // fmt.Println(greet(person1)) 54 | fmt.Println(person1.greet()) 55 | 56 | // use pointer method 57 | person1.hasBirthday() 58 | 59 | fmt.Println(person1.age) 60 | 61 | person2.getMarried("Williams") 62 | fmt.Println(person2.greet()) 63 | 64 | } 65 | -------------------------------------------------------------------------------- /testing/calc/operations.go: -------------------------------------------------------------------------------- 1 | package calc 2 | 3 | func Add(x int, y int) int { 4 | return x + y 5 | } 6 | 7 | func Subtract(x int, y int) int { 8 | return x - y 9 | } 10 | -------------------------------------------------------------------------------- /testing/calc/operations_test.go: -------------------------------------------------------------------------------- 1 | package calc 2 | 3 | import "testing" 4 | 5 | func TestAdd(t *testing.T) { 6 | var result int = Add(10, 20) 7 | if result != 30 { 8 | t.Error("Expected 30, got ", result) 9 | } 10 | } 11 | 12 | func TestSubtract(t *testing.T) { 13 | var result int = Subtract(10, 20) 14 | if result != -10 { 15 | t.Error("Expected -10, got ", result) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /variables/constants.go: -------------------------------------------------------------------------------- 1 | package variables 2 | 3 | import "fmt" 4 | 5 | const GAME_OVER = false; 6 | 7 | func Constants() { 8 | const PI, E = 3.14, 2.7182 9 | 10 | fmt.Println(PI) 11 | fmt.Println(E) 12 | fmt.Println(GAME_OVER) 13 | } 14 | -------------------------------------------------------------------------------- /variables/declare_variables.go: -------------------------------------------------------------------------------- 1 | package variables 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func DeclareVariables() { 8 | // declare a variable 9 | var message string 10 | message = "My secret message" 11 | fmt.Println(message) 12 | 13 | // declaring and assigning a variable 14 | var myName string = "Fazt" 15 | fmt.Println(myName) 16 | 17 | // you can do this with any type 18 | var price float32 = 30.5 19 | fmt.Println(price) 20 | 21 | var isCool bool = true 22 | fmt.Println(isCool) 23 | 24 | var year int = 2022 25 | fmt.Println(year) 26 | 27 | // error if not use the variable 28 | // var n = 2020 29 | // var j int = 10 30 | 31 | // multiple declarations 32 | var name, lastname, age = "Joe", "Harper", 33 33 | fmt.Println(name, lastname, age) 34 | 35 | var ( 36 | username = "fazt" 37 | email = "fazt@mail.com" 38 | password = "123456" 39 | ) 40 | fmt.Println(username, email, password) 41 | 42 | } 43 | -------------------------------------------------------------------------------- /variables/outside_variables.go: -------------------------------------------------------------------------------- 1 | package variables 2 | 3 | import "fmt" 4 | 5 | // variable in package level 6 | var i int = 30 7 | 8 | // or declared variable outside a function 9 | var username string = "Fazt" 10 | 11 | // grouping variables 12 | var ( 13 | characterName string = "Sherlock Holmes" 14 | proffession string = "detective" 15 | age int = 30 16 | city = "London" // type inference 17 | ) 18 | 19 | // var theUrl string = "http://google.com" 20 | var theURL string = "http://google.com" 21 | var myHTTP string = "" 22 | 23 | var ( 24 | counter int = 0 25 | ) 26 | 27 | func OutsideVariables() { 28 | fmt.Printf("%v %T\n", i, i) 29 | fmt.Printf("%v %T\n", username, username) 30 | counter++ 31 | counter++ 32 | counter++ 33 | 34 | fmt.Println(characterName, proffession, age, city, theURL, myHTTP, counter) 35 | } 36 | -------------------------------------------------------------------------------- /variables/reasign_variable.go: -------------------------------------------------------------------------------- 1 | package variables 2 | 3 | import "fmt" 4 | 5 | func reasignVariable() { 6 | // reassign a variable 7 | var year int = 2022 8 | year = 2023 9 | fmt.Println(year) 10 | 11 | var city string = "Londong" 12 | city = "Paris" 13 | fmt.Println(city) 14 | 15 | // reasign with another type 16 | // var b int = 200 17 | // b = "30" // error 18 | } 19 | -------------------------------------------------------------------------------- /variables/reusability.go: -------------------------------------------------------------------------------- 1 | package variables 2 | 3 | import "fmt" 4 | 5 | func Reusability() { 6 | var x int = 100 7 | 8 | fmt.Println("x =", x) 9 | fmt.Println("x + 100 =", x + 100) 10 | fmt.Println("x - 50 =", x - 50) 11 | fmt.Println("x * 2 =", x * 2) 12 | fmt.Println("x / 2 =", x / 2) 13 | 14 | fmt.Println("x =", x) 15 | 16 | // reassigning a variable 17 | var y int = 100 18 | 19 | fmt.Println("\ny =", y) 20 | y = y + 100 21 | y = y - 50 22 | y = y * 2 23 | y = y / 2 24 | fmt.Println("y =", y) 25 | } 26 | -------------------------------------------------------------------------------- /variables/shadowing.go: -------------------------------------------------------------------------------- 1 | package variables 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | 8 | func shadowing() { 9 | fmt.Println(i) 10 | // var i int = 70 11 | i := 70 12 | fmt.Println(i) 13 | } 14 | -------------------------------------------------------------------------------- /variables/shorthand_declaration.go: -------------------------------------------------------------------------------- 1 | package variables 2 | 3 | func Shorthand() { 4 | // shorthands declaration varaibles for functions 5 | // this cannot be declared outside function 6 | // fullName := "Joe McMillan" 7 | firstName := "Joe" 8 | println(firstName) 9 | 10 | lastName := "McMillan" 11 | println(lastName) 12 | 13 | // multiple declarations 14 | // emailOne := "fazt@faztweb.com" 15 | // emailTwo := "fazttech@gmail.com" 16 | emailOne, emailTwo := "fazt@faztweb.com", "fazttech@gmail.com" 17 | 18 | // you can print multiple variables too 19 | println(emailOne, emailTwo) 20 | } 21 | -------------------------------------------------------------------------------- /variables/static_typing.go: -------------------------------------------------------------------------------- 1 | package variables 2 | 3 | import "fmt" 4 | 5 | func StaticTyping() { 6 | // var b int = 200 7 | // b = "fazt" // error 8 | // fmt.Println(b) 9 | 10 | var name string = "Fazt" 11 | var age int = 30 12 | var score float32 = 30.5 13 | var isAlive bool = true 14 | 15 | // print the datatypes of variables 16 | fmt.Printf("%T\n", name) 17 | fmt.Printf("%T\n", age) 18 | fmt.Printf("%T\n", isAlive) 19 | fmt.Printf("%T\n", score) 20 | 21 | // print the variables and its datatypes 22 | fmt.Printf("%v %T\n", name, name) 23 | fmt.Printf("%v %T\n", age, age) 24 | fmt.Printf("%v %T\n", isAlive, isAlive) 25 | fmt.Printf("%v %T\n", score, score) 26 | } 27 | -------------------------------------------------------------------------------- /variables/type_inference.go: -------------------------------------------------------------------------------- 1 | package variables 2 | 3 | import "fmt" 4 | 5 | func TypeInference() { 6 | // type inference 7 | // var name = "Ryan" 8 | // var lastname = "Ray" 9 | // var user_age = 33 10 | // var isAlive = true 11 | // var score float32 = 152.30 12 | 13 | name := "Ryan" 14 | user_age := 33 15 | isAlive := true 16 | score := 152.30 17 | 18 | fmt.Println(name) 19 | fmt.Println(isAlive) 20 | fmt.Println(score) 21 | fmt.Println(user_age) 22 | 23 | fmt.Printf("%T\n", name) 24 | fmt.Printf("%T\n", isAlive) 25 | fmt.Printf("%T\n", score) 26 | fmt.Printf("%T\n", user_age) 27 | 28 | } 29 | --------------------------------------------------------------------------------