├── 01_hello └── main.go ├── 03_packages ├── strutil │ └── reverse.go └── main.go ├── 04_functions └── main.go ├── 11_closures └── main.go ├── 10_pointers └── main.go ├── 14_web └── main.go ├── 05_arrays_slices └── main.go ├── 07_loops └── main.go ├── 08_maps └── main.go ├── 02_vars └── main.go ├── 09_range └── main.go ├── 06_conditionals └── main.go ├── 13_interfaces └── main.go └── 12_structs └── main.go /01_hello/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("Hello World") 7 | } 8 | -------------------------------------------------------------------------------- /03_packages/strutil/reverse.go: -------------------------------------------------------------------------------- 1 | package strutil 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 | -------------------------------------------------------------------------------- /04_functions/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func greeting(name string) string { 6 | return "Hello " + name 7 | } 8 | 9 | func getSum(num1, num2 int) int { 10 | return num1 + num2 11 | } 12 | 13 | func main() { 14 | fmt.Println(getSum(3, 4)) 15 | } 16 | -------------------------------------------------------------------------------- /11_closures/main.go: -------------------------------------------------------------------------------- 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 | } 19 | -------------------------------------------------------------------------------- /03_packages/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | 7 | "github.com/bradtraversy/go_crash_course/03_packages/strutil" 8 | ) 9 | 10 | func main() { 11 | fmt.Println(math.Floor(2.7)) 12 | fmt.Println(math.Ceil(2.7)) 13 | fmt.Println(math.Sqrt(16)) 14 | fmt.Println(strutil.Reverse("hello")) 15 | } 16 | -------------------------------------------------------------------------------- /10_pointers/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | a := 5 7 | b := &a 8 | 9 | fmt.Println(a, b) 10 | fmt.Printf("%T\n", b) 11 | 12 | // Use * to read val from address 13 | fmt.Println(*b) 14 | fmt.Println(*&a) 15 | 16 | // Change val with pointer 17 | *b = 10 18 | fmt.Println(a) 19 | } 20 | -------------------------------------------------------------------------------- /14_web/main.go: -------------------------------------------------------------------------------- 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, "

Hello World

") 10 | } 11 | 12 | func about(w http.ResponseWriter, r *http.Request) { 13 | fmt.Fprintf(w, "

About

") 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 | } 22 | -------------------------------------------------------------------------------- /05_arrays_slices/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | // Arrays 9 | // var fruitArr [2]string 10 | 11 | // // Assign values 12 | // fruitArr[0] = "Apple" 13 | // fruitArr[1] = "Orange" 14 | 15 | // Decalre and assign 16 | // fruitArr := [2]string{"Apple", "Orange"} 17 | 18 | // fmt.Println(fruitArr) 19 | // fmt.Println(fruitArr[1]) 20 | 21 | fruitSlice := []string{"Apple", "Orange", "Grape", "Cherry"} 22 | 23 | fmt.Println(len(fruitSlice)) 24 | fmt.Println(fruitSlice[1:3]) 25 | } 26 | -------------------------------------------------------------------------------- /07_loops/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | // Long method 7 | i := 1 8 | for i <= 10 { 9 | fmt.Println(i) 10 | // i = i + 1 11 | i++ 12 | } 13 | 14 | // Short method 15 | for i := 1; i <= 10; i++ { 16 | fmt.Printf("Number %d\n", i) 17 | } 18 | 19 | // FizzBuzz 20 | for i := 1; i <= 100; i++ { 21 | if i%15 == 0 { 22 | fmt.Println("FizzBuzz") 23 | } else if i%3 == 0 { 24 | fmt.Println("Fizz") 25 | } else if i%5 == 0 { 26 | fmt.Println("Buzz") 27 | } else { 28 | fmt.Println(i) 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /08_maps/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | // Define map 7 | // emails := make(map[string]string) 8 | 9 | // Assign kv 10 | // emails["Bob"] = "bob@gmail.com" 11 | // emails["Sharon"] = "sharon@gmail.com" 12 | // emails["Mike"] = "mike@gmail.com" 13 | 14 | // Decalre map and add kv 15 | emails := map[string]string{"Bob": "bob@gmail.com", "Sharon": "sharon@gmail.com"} 16 | 17 | emails["Mike"] = "mike@gmail.com" 18 | 19 | fmt.Println(emails) 20 | fmt.Println(len(emails)) 21 | fmt.Println(emails["Bob"]) 22 | 23 | // Delete from map 24 | delete(emails, "Bob") 25 | fmt.Println(emails) 26 | } 27 | -------------------------------------------------------------------------------- /02_vars/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | // MAIN TYPES 7 | // string 8 | // bool 9 | // int 10 | // int int8 int16 int32 int64 11 | // uint uint8 uint16 uint32 uint64 uintptr 12 | // byte - alias for uint8 13 | // rune - alias for int32 14 | // float32 float64 15 | // complex64 complex128 16 | 17 | // Using var 18 | // var name = "Brad" 19 | var age int32 = 37 20 | const isCool = true 21 | var size float32 = 2.3 22 | 23 | // Shorthand 24 | // name := "Brad" 25 | // email := "brad@gmail.com" 26 | 27 | name, email := "Brad", "brad@gmail.com" 28 | 29 | fmt.Println(name, age, isCool, email) 30 | fmt.Printf("%T\n", size) 31 | } 32 | -------------------------------------------------------------------------------- /09_range/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | ids := []int{33, 76, 54, 23, 11, 2} 7 | 8 | // Loop through ids 9 | for i, id := range ids { 10 | fmt.Printf("%d - ID: %d\n", i, id) 11 | } 12 | 13 | // Not using index 14 | for _, id := range ids { 15 | fmt.Printf("ID: %d\n", id) 16 | } 17 | 18 | // Add ids together 19 | sum := 0 20 | for _, id := range ids { 21 | sum += id 22 | } 23 | fmt.Println("Sum", sum) 24 | 25 | // Range with map 26 | emails := map[string]string{"Bob": "bob@gmail.com", "Sharon": "sharon@gmail.com"} 27 | 28 | for k, v := range emails { 29 | fmt.Printf("%s: %s\n", k, v) 30 | } 31 | 32 | for k := range emails { 33 | fmt.Println("Name: " + k) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /06_conditionals/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | x := 10 7 | y := 10 8 | 9 | // If else 10 | if x <= y { 11 | fmt.Printf("%d is less than or equal to %d\n", x, y) 12 | } else { 13 | fmt.Printf("%d is less than %d\n", y, x) 14 | } 15 | 16 | // else if 17 | color := "red" 18 | 19 | if color == "red" { 20 | fmt.Println("color is red") 21 | } else if color == "blue" { 22 | fmt.Println("color is blue") 23 | } else { 24 | fmt.Println("color is NOT blue or red") 25 | } 26 | 27 | // Switch 28 | switch color { 29 | case "red": 30 | fmt.Println("color is red") 31 | case "blue": 32 | fmt.Println("color is blue") 33 | default: 34 | fmt.Println("color is NOT blue or red") 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /13_interfaces/main.go: -------------------------------------------------------------------------------- 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("Rectangle Area: %f\n", getArea(rectangle)) 39 | } 40 | -------------------------------------------------------------------------------- /12_structs/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | // Define person struct 9 | type Person struct { 10 | // firstName string 11 | // lastName string 12 | // city string 13 | // gender string 14 | // age int 15 | 16 | firstName, lastName, city, gender string 17 | age int 18 | } 19 | 20 | // Greeting method (value reciever) 21 | func (p Person) greet() string { 22 | return "Hello, my name is " + p.firstName + " " + p.lastName + " and I am " + strconv.Itoa(p.age) 23 | } 24 | 25 | // hasBirthday method (pointer reciever) 26 | func (p *Person) hasBirthday() { 27 | p.age++ 28 | } 29 | 30 | // getMarried (pointer reciever) 31 | func (p *Person) getMarried(spouseLastName string) { 32 | if p.gender == "m" { 33 | return 34 | } else { 35 | p.lastName = spouseLastName 36 | } 37 | } 38 | 39 | func main() { 40 | // Init person using struct 41 | person1 := Person{firstName: "Samantha", lastName: "Smith", city: "Boston", gender: "f", age: 25} 42 | // Alternative 43 | person2 := Person{"Bob", "Johnson", "New York", "m", 30} 44 | 45 | // fmt.Println(person1.firstName) 46 | // person1.age++ 47 | // fmt.Println(person1) 48 | 49 | person1.hasBirthday() 50 | person1.getMarried("Williams") 51 | 52 | person2.getMarried("Thompson") 53 | 54 | fmt.Println(person2.greet()) 55 | } 56 | --------------------------------------------------------------------------------