├── README.md ├── array └── main.go ├── crud └── main.go ├── data-conv └── main.go ├── defer └── main.go ├── error_handling └── main.go ├── example.txt ├── file └── main.go ├── for-loop └── main.go ├── function └── main.go ├── go.mod ├── goroutine └── main.go ├── if-else └── main.go ├── json └── main.go ├── main.go ├── map └── main.go ├── myutil └── myutil.go ├── pointer └── main.go ├── print └── main.go ├── slice └── main.go ├── strings └── main.go ├── struct └── main.go ├── switch └── main.go ├── sync_waitgroup └── main.go ├── test └── main.go ├── time └── main.go ├── url └── main.go ├── userInput └── main.go └── webreq └── main.go /README.md: -------------------------------------------------------------------------------- 1 | # GoLang Course Overview 2 | 3 | Featured_1280x720 4 | 5 | This course covers various topics in GoLang, providing a comprehensive guide to learning the language. Each topic is organized into folders for easy navigation. 6 | 7 | ## Topics Covered: 8 | 9 | - **Array**: Learn about arrays in GoLang, including CRUD operations. 10 | - **Data Conversion**: Understand data conversion techniques in GoLang. 11 | - **Defer**: Explore the `defer` keyword and its usage in GoLang. 12 | - **Error Handling**: Learn how to handle errors effectively in GoLang. 13 | - **File Operations**: Discover how to perform file operations in GoLang. 14 | - **For Loop**: Understand the `for` loop and its variations in GoLang. 15 | - **Function**: Explore functions in GoLang, including declaration and usage. 16 | - **Goroutine**: Learn about goroutines and concurrent programming in GoLang. 17 | - **sync.WaitGroup**: Learn about WaitGroup and it's waiting property for goroutines. 18 | - **If-Else**: Understand conditional statements (`if-else`) in GoLang. 19 | - **JSON**: Learn how to work with JSON data in GoLang. 20 | - **Map**: Explore the `map` data structure in GoLang. 21 | - **Pointer**: Understand pointers and their usage in GoLang. 22 | - **Print**: Learn how to print output in GoLang. 23 | - **Slice**: Understand slices and their manipulation in GoLang. 24 | - **Strings**: Learn about string manipulation in GoLang. 25 | - **Struct**: Explore structures and their usage in GoLang. 26 | - **Switch**: Understand the `switch` statement in GoLang. 27 | - **Testing**: Learn about writing tests in GoLang. 28 | - **Time**: Understand time and date manipulation in GoLang. 29 | - **URL**: Learn how to work with URLs in GoLang. 30 | - **User Input**: Understand how to take user input in GoLang. 31 | - **Web Requests**: Learn how to make web requests in GoLang. 32 | 33 | ## How to Use: 34 | 35 | Each folder contains code examples and explanations related to the topic. Simply navigate to the folder of interest to access the content. 36 | 37 | ## Additional Notes: 38 | 39 | - The course assumes basic knowledge of programming concepts. 40 | - Feel free to explore the topics in any order based on your learning preferences. 41 | 42 | --- 43 | 44 | Happy coding! 45 | -------------------------------------------------------------------------------- /array/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("we are learning Array in Golang") 7 | 8 | // var name[5]string 9 | 10 | // // 0 1 2 3 4 11 | // name[0] = "prince" 12 | // name[2] = "Aakash" 13 | 14 | // fmt.Println("Names of Person is :", name) 15 | 16 | // var numbers = [8]int{1, 2, 3, 4, 5} 17 | // fmt.Println("Number is :", numbers) 18 | 19 | // fmt.Println("Length of Numbers array is :", len(numbers)) 20 | 21 | // fmt.Println("value of name at 2 index is :", name[2]) 22 | 23 | // "" 24 | var name[5]string 25 | name[2] = "Prince" 26 | name[0] = "aaksh" 27 | 28 | fmt.Println("name is :", name) 29 | fmt.Printf("name Array is %q\n", name) 30 | } -------------------------------------------------------------------------------- /crud/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "io/ioutil" 7 | "net/http" 8 | "strings" 9 | ) 10 | 11 | type Todo struct { 12 | UserID int `json:"userId"` 13 | Id int `json:"id"` 14 | Title string `json:"title"` 15 | Completed bool `json:"completed"` 16 | } 17 | 18 | func performGetRequest() { 19 | res, err := http.Get("https://jsonplaceholder.typicode.com/todos/1") 20 | if err != nil { 21 | fmt.Println("Error getting : ", err) 22 | return 23 | } 24 | defer res.Body.Close() 25 | 26 | if res.StatusCode != http.StatusOK { 27 | fmt.Println("Error in getting Response: ", res.Status) 28 | return 29 | } 30 | 31 | // data, err := ioutil.ReadAll(res.Body) 32 | // if err != nil { 33 | // fmt.Println("Error reading : ", err) 34 | // return 35 | // } 36 | // fmt.Println("Data: ", string(data)) 37 | 38 | var todo Todo 39 | err = json.NewDecoder(res.Body).Decode(&todo) 40 | if err != nil { 41 | fmt.Println("Error decoding : ", err) 42 | return 43 | } 44 | fmt.Println("Todo: ", todo) 45 | 46 | fmt.Println("Title response : ", todo.Title) 47 | fmt.Println("completed response : ", todo.Completed) 48 | } 49 | 50 | func performPostRequest() { 51 | todo := Todo{ 52 | UserID: 23, 53 | Title: "Prince Kumar", 54 | Completed: true, 55 | } 56 | 57 | // Convert the Todo struct to JSON 58 | jsonData, err := json.Marshal(todo) 59 | if err != nil { 60 | fmt.Println("Error marshalling : ", err) 61 | return 62 | } 63 | 64 | // convert json data to string 65 | jsonString := string(jsonData) 66 | 67 | // convert string data to reader 68 | jsonReader := strings.NewReader(jsonString) 69 | 70 | myURL := "https://jsonplaceholder.typicode.com/todos" 71 | 72 | // send POST request 73 | res, err := http.Post(myURL, "application/json", jsonReader) 74 | if err != nil { 75 | fmt.Println("Error sending request : ", err) 76 | return 77 | } 78 | 79 | defer res.Body.Close() 80 | 81 | // data, _ := ioutil.ReadAll(res.Body) 82 | // fmt.Println("Response : ", string(data)) 83 | 84 | fmt.Println("Response status : ", res.Status) 85 | } 86 | 87 | func performUpdateRequest() { 88 | todo := Todo{ 89 | UserID: 23789, 90 | Title: "Prince Kumar Golang hello World", 91 | Completed: false, 92 | } 93 | 94 | // Convert the Todo struct to JSON 95 | jsonData, err := json.Marshal(todo) 96 | if err != nil { 97 | fmt.Println("Error marshalling : ", err) 98 | return 99 | } 100 | 101 | // convert json data to string 102 | jsonString := string(jsonData) 103 | 104 | // convert string data to reader 105 | jsonReader := strings.NewReader(jsonString) 106 | 107 | const myurl = "https://jsonplaceholder.typicode.com/todos/1" 108 | 109 | // create PUT Request 110 | 111 | req, err := http.NewRequest(http.MethodPut, myurl, jsonReader) 112 | if err != nil { 113 | fmt.Println("Error creating PUT Request : ", err) 114 | return 115 | } 116 | req.Header.Set("Content-type", "application/json") 117 | 118 | // Send the request 119 | client := http.Client{} 120 | res, err := client.Do(req) 121 | if err != nil { 122 | fmt.Println("Error sending request : ", err) 123 | return 124 | } 125 | defer res.Body.Close() 126 | 127 | data, _ := ioutil.ReadAll(res.Body) 128 | fmt.Println("Response : ", string(data)) 129 | fmt.Println("Response status : ", res.Status) 130 | } 131 | 132 | func performDeleteRequest() { 133 | const myurl = "https://jsonplaceholder.typicode.com/todos/1" 134 | 135 | // create DELETE Request 136 | 137 | req, err := http.NewRequest(http.MethodDelete, myurl, nil) 138 | if err != nil { 139 | fmt.Println("Error creating Delete Request : ", err) 140 | return 141 | } 142 | 143 | // Send the request 144 | client := http.Client{} 145 | res, err := client.Do(req) 146 | if err != nil { 147 | fmt.Println("Error sending request : ", err) 148 | return 149 | } 150 | defer res.Body.Close() 151 | 152 | fmt.Println("Response status : ", res.Status) 153 | } 154 | 155 | func main() { 156 | fmt.Println("Learning CRUD...") 157 | // performGetRequest() 158 | // performPostRequest() 159 | // performUpdateRequest() 160 | performDeleteRequest() 161 | } 162 | -------------------------------------------------------------------------------- /data-conv/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strconv" 6 | ) 7 | 8 | func main() { 9 | var num int = 42 10 | fmt.Println("Number is ", num) 11 | fmt.Printf("Type of num is %T\n", num) 12 | 13 | var data float64 = float64(num) 14 | data = data + 1.23 15 | fmt.Println("Data is ", data) 16 | fmt.Printf("Type of Data is %T\n", data) 17 | 18 | num = 123 19 | str := strconv.Itoa(num) 20 | fmt.Println("str is ", str) 21 | fmt.Printf("Type of str is %T\n", str) 22 | 23 | number_string := "1234" 24 | number_int, _ := strconv.Atoi(number_string) 25 | number_int = number_int + 262892 26 | fmt.Println("number_int is ", number_int) 27 | fmt.Printf("Type of number_int is %T\n", number_int) 28 | 29 | num_string := "3.14" 30 | number_float, _ := strconv.ParseFloat(num_string, 64) 31 | fmt.Println("number_float is ", number_float) 32 | fmt.Printf("Type of number_float is %T\n", number_float) 33 | 34 | } 35 | -------------------------------------------------------------------------------- /defer/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func add(a, b int) int { 6 | return a + b 7 | } 8 | 9 | func main() { 10 | fmt.Println("Starting of the program") 11 | data := add(5, 6) 12 | defer fmt.Println("Data is : ", data) 13 | defer fmt.Println("Middle of the program") 14 | fmt.Println("End of the program") 15 | } 16 | 17 | // Middle of the program 18 | // Data is : ", data 19 | 20 | // fmt.Println("Middle of the program") 21 | // fmt.Println(") -------------------------------------------------------------------------------- /error_handling/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | // func divide(a, b float64) (float64, error) { 6 | // if b == 0 { 7 | // return 0, fmt.Errorf("denominator must not be zero") 8 | // } 9 | // return a / b, nil 10 | // } 11 | 12 | func divide(a, b float64) (float64, string) { 13 | if b == 0 { 14 | return 0, "denominator must not be zero" 15 | } 16 | return a / b, "nil" 17 | } 18 | 19 | func main() { 20 | fmt.Println("started Error Handling in the functions") 21 | // ans, err := divide(10, 0) 22 | // if err != nil { 23 | // fmt.Println("Error Handling") 24 | // } 25 | // fmt.Println("Division of two numbers is ", ans) 26 | 27 | ans, _ := divide(10, 2) 28 | fmt.Println("Division of two numbers is ", ans) 29 | } -------------------------------------------------------------------------------- /example.txt: -------------------------------------------------------------------------------- 1 | hello world by prince 2 | Hi Bro 3 | Please Like and commment this video -------------------------------------------------------------------------------- /file/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func main() { 9 | 10 | /* 11 | file, err := os.Create("example.txt") 12 | if err != nil { 13 | fmt.Println("Error while creating file: ", err) 14 | return 15 | } 16 | defer file.Close() 17 | 18 | content := "hello world by prince" 19 | byte, errors := io.WriteString(file, content+"\n") 20 | fmt.Println("byte written: ", byte) 21 | if errors != nil { 22 | fmt.Println("Error while writing file: ", errors) 23 | return 24 | } 25 | 26 | fmt.Println("sucessfully created file") 27 | 28 | */ 29 | 30 | /* 31 | file, err := os.Open("example.txt") 32 | if err != nil { 33 | fmt.Println("Error while opening file: ", err) 34 | return 35 | } 36 | defer file.Close() 37 | 38 | // Create a buffer to read the file content 39 | buffer := make([]byte, 1024) 40 | 41 | // Read the file content into the buffer 42 | for { 43 | n, err := file.Read(buffer) 44 | if err == io.EOF { 45 | break 46 | } 47 | if err != nil { 48 | fmt.Println("Error while reading file", err) 49 | return 50 | } 51 | 52 | // Process the read content 53 | fmt.Println(string(buffer[:n])) 54 | } 55 | */ 56 | 57 | // Read the entire file into a byte slice 58 | content, err := os.ReadFile("example.txt") 59 | if err != nil { 60 | fmt.Println("Error while reading file ", err) 61 | return 62 | } 63 | fmt.Println(string(content)) 64 | } 65 | -------------------------------------------------------------------------------- /for-loop/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | for i := 0; i < 1; i++ { 7 | fmt.Println("Numbers is :", i) 8 | } 9 | 10 | counter := 0 11 | for { 12 | fmt.Println("Infinite Loop") 13 | counter++ 14 | if counter == 1 { 15 | break 16 | } 17 | } 18 | 19 | numbers := []int{11, 42, 83, 14, 75} 20 | for index, value := range numbers { 21 | fmt.Printf("Index of Numbers is %d, and value is %d\n", index, value) 22 | } 23 | 24 | data := "Hello, world!" 25 | for index, value := range data { 26 | fmt.Printf("Index of Data is %d, and value is %c\n", index, value) 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /function/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func simpleFunction() { 6 | fmt.Println("simple function") 7 | } 8 | 9 | func add(a int, b int) (result int) { 10 | result = a + b 11 | return 12 | } 13 | 14 | func multiply(a, b int) (result int) { 15 | result = a * b 16 | return 17 | } 18 | 19 | func main() { 20 | fmt.Println("we are learning function in Golang") 21 | simpleFunction() 22 | 23 | ans := add(3, 4) 24 | fmt.Println("add of two number is :", ans) 25 | 26 | data := multiply(3, 4) 27 | fmt.Println("multiplication of two numbers is :", data) 28 | } 29 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module mylearning 2 | 3 | go 1.21.1 4 | -------------------------------------------------------------------------------- /goroutine/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func sayHello() { 9 | fmt.Println("Hello, world!") 10 | // time.Sleep(2000 * time.Millisecond) // Simulating some work 11 | fmt.Println("sayHello function ended successfully") 12 | } 13 | 14 | func sayHi() { 15 | fmt.Println("Hi Prince :)") 16 | time.Sleep(1000 * time.Millisecond) // Simulating some work 17 | fmt.Println("Hi Prince Function ended:)") 18 | } 19 | 20 | func main() { 21 | fmt.Println("learning goroutines") 22 | 23 | go sayHello() 24 | go sayHi() 25 | 26 | // Wait for a moment to allow the goroutine to finish 27 | time.Sleep(800 * time.Millisecond) 28 | } 29 | -------------------------------------------------------------------------------- /if-else/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | x := 21 7 | if x > 5 { 8 | fmt.Println("x is greater than 5") 9 | } else { 10 | fmt.Println("x is smaller than 5") 11 | } 12 | 13 | z := 10 14 | if z > 10 { 15 | fmt.Println("z is greater than 10") 16 | } else if z > 5 { 17 | fmt.Println("z is greater than 5 but smaller than 10") 18 | } else { 19 | fmt.Println("z is smaller than 5") 20 | } 21 | 22 | y := 10 23 | if x < 5 && (y > 5 || z < 43) { 24 | fmt.Println("hey how are you ?") 25 | } else { 26 | fmt.Println("Learn programming with Hello World") 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /json/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | ) 7 | 8 | type Person struct { 9 | Name string `json:"name"` 10 | Age int `json:"age"` 11 | IsAdult bool `json:"is_adult"` 12 | } 13 | 14 | func main() { 15 | fmt.Println("we are learning JSON") 16 | person := Person{Name: "John", Age: 34, IsAdult: true} 17 | // fmt.Println("person Data is : ", person) 18 | 19 | // convert person into JSON Encoding (Marshalling) 20 | jsonData, err := json.Marshal(person) 21 | if err != nil { 22 | fmt.Println("Error marshalling ", err) 23 | return 24 | } 25 | fmt.Println("person Data is : ", string(jsonData)) 26 | 27 | // Decoding (Unmarshalling) 28 | var personData Person 29 | err = json.Unmarshal(jsonData, &personData) 30 | if err != nil { 31 | fmt.Println("Error unmarshalling ", err) 32 | return 33 | } 34 | fmt.Println("person Data is after unmarshalling: ", personData) 35 | 36 | } 37 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | fmt.Println("Learn Go Language by Hello World") 9 | 10 | // var name string = "Prince" 11 | // var version = 76 12 | // fmt.Println(name) 13 | // fmt.Println(version) 14 | 15 | // var money int = 67000 16 | // var currency = 3489 17 | // fmt.Println(money) 18 | // fmt.Println("currency: ", currency) 19 | 20 | // var dimension float64 = 87.12 21 | // fmt.Println(dimension) 22 | 23 | // var decided bool = false 24 | // decided = true 25 | // fmt.Println(decided) 26 | 27 | // var person = 23 28 | // fmt.Println(person) 29 | 30 | // const pi = 67.12 31 | // fmt.Println(pi) 32 | 33 | 34 | person := "Prince Agarwal" 35 | fmt.Println(person) 36 | 37 | 38 | var Public = "data is important" 39 | var private = "data is private"; 40 | 41 | fmt.Println(Public) 42 | fmt.Println(private) 43 | } 44 | -------------------------------------------------------------------------------- /map/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | 7 | // name <-> grade 8 | studentGrades := make(map[string]int) 9 | 10 | studentGrades["Prince"] = 100 11 | studentGrades["Alice"] = 90 12 | studentGrades["Bob"] = 85 13 | studentGrades["Charlie"] = 95 14 | 15 | fmt.Println("Marks of Bob : ", studentGrades["Bob"]) 16 | studentGrades["Bob"] = 100 17 | fmt.Println("new Marks of Bob : ", studentGrades["Bob"]) 18 | 19 | delete(studentGrades, "Bob") 20 | fmt.Println("Marks of Bob : ", studentGrades["Bob"]) 21 | 22 | // Checking if a key exists 23 | grades, exists := studentGrades["David"] 24 | fmt.Println("Grades of David : ", grades) 25 | fmt.Println("Davis exists : ", exists) 26 | 27 | // fmt.Println("Marks of David : ", studentGrades["David"]) 28 | 29 | // Checking if a key exists 30 | Grades, Exists := studentGrades["Prince"] 31 | fmt.Println("Grades of Prince : ", Grades) 32 | fmt.Println("Prince exists : ", Exists) 33 | 34 | for index, value := range studentGrades { 35 | fmt.Printf("Key is %s and marks is %d\n", index, value) 36 | } 37 | 38 | person := map[string]int{ 39 | "Alice": 90, 40 | "Bob": 85, 41 | "Charlie": 95, 42 | } 43 | 44 | for index, value := range person { 45 | fmt.Printf("---------Key is %s and marks is %d\n", index, value) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /myutil/myutil.go: -------------------------------------------------------------------------------- 1 | package myutil 2 | 3 | import "fmt" 4 | 5 | func PrintMessage(message string) { 6 | fmt.Println(message) 7 | } 8 | -------------------------------------------------------------------------------- /pointer/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func modifyValueByReference(num *int) { 6 | *num = *num + 20 7 | } 8 | 9 | func main() { 10 | // var num int 11 | num := "Prince" 12 | 13 | // var ptr *int 14 | // ptr = &num 15 | 16 | ptr := &num 17 | 18 | // fmt.Println("Num has value: ", num) 19 | fmt.Println("pointer contains: ", ptr) 20 | fmt.Println("Data contains through Pointer: ", *ptr) 21 | 22 | var pointer *int // default pointer == nil 23 | if pointer == nil { 24 | fmt.Println("Pointer is not assigned") 25 | } 26 | 27 | value := 5 28 | modifyValueByReference(&value) 29 | fmt.Println("Value contains : ", value) 30 | 31 | } 32 | -------------------------------------------------------------------------------- /print/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | age := 25 7 | name := "Alice" 8 | height := 5.8234567 9 | 10 | fmt.Println("age: ", age, "height: ", height, "name: ", name) 11 | //age: 25height: 5.8234567name: Alice 12 | fmt.Println("Hello world") 13 | 14 | // fmt.Printf("age is %d\n", age) 15 | // fmt.Printf("height is %.2f\n", height) 16 | // fmt.Printf("Type of age is %T\n", age) 17 | // fmt.Printf("Type of height is %T\n", height) 18 | 19 | fmt.Printf("Age is %d\n", age) 20 | fmt.Printf("Name: %s, Age: %d, Height: %.2f\n", name, age, height) 21 | } 22 | -------------------------------------------------------------------------------- /slice/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | 9 | // numbers := []int{1, 2} 10 | // numbers = append(numbers, 3, 10, 12, 13, 14, 15, 16, 17, 18, 19) 11 | // fmt.Println("Number : ", numbers) 12 | // fmt.Printf("Number has data type : %T\n", numbers) 13 | // fmt.Println("Length : ", len(numbers)) 14 | 15 | // name := []string{} 16 | // fmt.Println("name : ", name) 17 | 18 | numbers := make([]int, 3, 5) 19 | 20 | numbers = append(numbers, 4) 21 | numbers = append(numbers, 98) 22 | numbers = append(numbers, 6) 23 | numbers = append(numbers, 4) 24 | numbers = append(numbers, 98) 25 | numbers = append(numbers, 6) 26 | numbers = append(numbers, 6) 27 | numbers = append(numbers, 6) 28 | 29 | fmt.Println("Slice:", numbers) 30 | fmt.Println("Length:", len(numbers)) 31 | fmt.Println("Capacity:", cap(numbers)) 32 | 33 | 34 | stock := make([]int, 0) 35 | fmt.Println("Slice:", stock) 36 | fmt.Println("Length:", len(stock)) 37 | fmt.Println("Capacity:", cap(stock)) 38 | 39 | } 40 | -------------------------------------------------------------------------------- /strings/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | ) 7 | 8 | func main() { 9 | data := "apple.orange.banana.prince" 10 | parts := strings.Split(data, ".") 11 | fmt.Println(parts) 12 | 13 | str := "one two three four two two five" 14 | count := strings.Count(str, "two") 15 | fmt.Println("count: ", count) 16 | 17 | str = " Hello, Go! " 18 | trimmed := strings.TrimSpace(str) 19 | fmt.Println("trimmed: ", trimmed) 20 | 21 | str1 := "Prince" 22 | str2 := "Agarwal" 23 | result := strings.Join([]string{str1, "Kumar", str2}, " ") 24 | fmt.Println("result: ", result) 25 | } 26 | -------------------------------------------------------------------------------- /struct/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | // int string bool Person Contact Address 6 | // var name Person 7 | type Person struct { 8 | FirstName string 9 | LastName string 10 | Age int 11 | } 12 | 13 | type Contact struct { 14 | Email string 15 | Phone string 16 | Fax string 17 | } 18 | 19 | type Address struct { 20 | House int 21 | Area string 22 | State string 23 | } 24 | 25 | type Employee struct { 26 | Person_Details Person 27 | Person_Contact Contact 28 | Person_Address Address 29 | } 30 | 31 | func main() { 32 | var prince Person 33 | // fmt.Println("Prince Person : ", prince) // " " " " 0 // __0 34 | prince.FirstName = "Prince" 35 | prince.LastName = "Agarwal" 36 | // fmt.Println("Prince Person : ", prince) 37 | 38 | // 2nd method 39 | person1 := Person{ 40 | FirstName: "Aakash", 41 | LastName: "Singh", 42 | Age: 26, 43 | } 44 | fmt.Println("Person 1 : ", person1) 45 | 46 | // new keyword 47 | var person2 = new(Person) 48 | person2.FirstName = "Simran" 49 | person2.LastName = "Agarwal" 50 | person2.Age = 26 51 | 52 | // fmt.Println("Person 2 : ", person2.FirstName) 53 | 54 | // fmt.Println("Age of Prince is ", prince.Age) 55 | 56 | var employee1 Employee 57 | employee1.Person_Details = Person{ 58 | FirstName: "Prince", 59 | LastName: "Agarwal", 60 | Age: 26, 61 | } 62 | employee1.Person_Contact.Email = "contact@gmail.com" 63 | employee1.Person_Contact.Phone = "98765432" 64 | 65 | employee1.Person_Address = Address{ 66 | House: 12, 67 | Area: "Ranchi", 68 | State: "Jharkhand", 69 | } 70 | employee1.Person_Contact.Fax = "Fax@4567890" 71 | fmt.Println("Employee 1 : ", employee1.Person_Contact) 72 | } 73 | -------------------------------------------------------------------------------- /switch/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | day := 2 7 | 8 | switch day { 9 | case 1, 30, 22, 19: 10 | fmt.Println("Good Day we are very fine") 11 | case 2: 12 | fmt.Println("Bad Day") 13 | case 3: 14 | fmt.Println("Wednesday") 15 | default: 16 | fmt.Println("Unknown Day") 17 | } 18 | 19 | month := "January" 20 | 21 | switch month { 22 | case "January", "February", "March": 23 | fmt.Println("Winter") 24 | case "April", "May", "June": 25 | fmt.Println("Spring") 26 | default: 27 | fmt.Println("Other season") 28 | } 29 | 30 | temperature := 10 31 | switch { 32 | case temperature < 0: 33 | fmt.Println("Freezing") 34 | case temperature >= 0 && temperature < 10: 35 | fmt.Println("Cold") 36 | case temperature >= 10 && temperature < 20: 37 | fmt.Println("Cool") 38 | case temperature >= 20 && temperature < 30: 39 | fmt.Println("Warm") 40 | default: 41 | fmt.Println("Hot") 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /sync_waitgroup/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "sync" 6 | ) 7 | 8 | func worker(i int, wg *sync.WaitGroup) { 9 | defer wg.Done() // Signal that this goroutine is done 10 | fmt.Printf("worker %d started\n", i) 11 | // some task is happening 12 | fmt.Printf("worker %d end\n", i) 13 | } 14 | 15 | // worker(1) 16 | // worker(2) 17 | // worker(3) 18 | 19 | func main() { 20 | // fmt.Println("Explore goroutine started") 21 | 22 | var wg sync.WaitGroup 23 | // Start 3 worker goroutines 24 | for i := 1; i <= 3; i++ { 25 | wg.Add(1) // Increment the WaitGroup counter 26 | go worker(i, &wg) 27 | } 28 | 29 | // Wait for all workers to finish 30 | wg.Wait() 31 | fmt.Println("workers task complete") 32 | 33 | } 34 | -------------------------------------------------------------------------------- /test/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func main() { 8 | fmt.Println("Subscribe to Hello World Youtube channel") 9 | } 10 | -------------------------------------------------------------------------------- /time/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | ) 7 | 8 | func main() { 9 | 10 | currentTime := time.Now() 11 | fmt.Println("Current time: ", currentTime) 12 | fmt.Printf("Type of currentTime %T\n", currentTime) 13 | 14 | formatted := currentTime.Format("2006/01/02, Monday") 15 | fmt.Println("Formatted time: ", formatted) 16 | 17 | layout_str := "02/01/2006" 18 | dateStr := "25/11/2030" 19 | formatted_time, _ := time.Parse(layout_str, dateStr) 20 | fmt.Println("Formatted time:", formatted_time) 21 | 22 | // add 1 more day in currentTime 23 | new_date := currentTime.Add(48 * time.Hour) 24 | fmt.Println("new_date time: ", new_date) 25 | formatted_new_date := new_date.Format("2006/01/02 Monday") 26 | fmt.Println("formatted_new_date time: ", formatted_new_date) 27 | 28 | } 29 | -------------------------------------------------------------------------------- /url/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/url" 6 | ) 7 | 8 | func main() { 9 | fmt.Println("Learning URL") 10 | myURL := "https://example.com:8080/path/to/resource?key1=value1&key2=value2" 11 | // fmt.Printf("Type of URL: %T\n", myURL) 12 | 13 | parsedURL, err := url.Parse(myURL) 14 | if err != nil { 15 | fmt.Println("Can't parse URL ", err) 16 | return 17 | } 18 | // fmt.Printf("Type of URL: %T\n", parsedURL) 19 | 20 | fmt.Println("Scheme: ", parsedURL.Scheme) 21 | fmt.Println("Host: ", parsedURL.Host) 22 | fmt.Println("Path: ", parsedURL.Path) 23 | fmt.Println("RawQuery: ", parsedURL.RawQuery) 24 | 25 | // Modifying URL components 26 | parsedURL.Path = "/newPath" 27 | parsedURL.RawQuery = "username=iamprince" 28 | 29 | // Constructing a URL string from a URL object 30 | newUrl := parsedURL.String() 31 | 32 | fmt.Println("new URL: ", newUrl) 33 | 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /userInput/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "os" 7 | ) 8 | 9 | func main() { 10 | fmt.Println("Hey, What's your name?") 11 | // var name string; 12 | // fmt.Scan(&name) 13 | // fmt.Println("Hello, Mr.", name); 14 | 15 | reader := bufio.NewReader(os.Stdin) 16 | name, _ := reader.ReadString('\n') 17 | fmt.Println("Hello, Mr.", name) 18 | } 19 | -------------------------------------------------------------------------------- /webreq/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "net/http" 7 | ) 8 | 9 | func main() { 10 | fmt.Println("Learning web service") 11 | res, err := http.Get("https://jsonplaceholder.typicode.com/todos/1") 12 | if err != nil { 13 | fmt.Println("Error getting GET response ", err) 14 | return 15 | } 16 | defer res.Body.Close() 17 | fmt.Printf("Type of response: %T\n", res) 18 | // fmt.Println("response: ", res) 19 | 20 | // Read the response body 21 | data, err := ioutil.ReadAll(res.Body) 22 | if err != nil { 23 | fmt.Println("Error reading response ", err) 24 | return 25 | } 26 | 27 | fmt.Println("response: ", string(data)) 28 | 29 | } 30 | --------------------------------------------------------------------------------