diff --git a/Go/Concepts/12) slices2.go b/Go/Concepts/12) slices2.go index 3112ef9..fd67fb5 100644 --- a/Go/Concepts/12) slices2.go +++ b/Go/Concepts/12) slices2.go @@ -8,7 +8,7 @@ func main() { fmt.Println(cars) var index int = 2 - cars = append(cars[:index], cars[index+1:]...) // remove the element at index 2. :index is from 0 to index-1, [index+1:] is from index+1 to end + cars = append(cars[:index], cars[index+1:]...) // remove the element at index 2. :index is from 0 to index-1, [index+1:] is from index+1 to end. We can append both elements as well as a slice. ... is to denote a slice fmt.Println(cars) } diff --git a/Go/Concepts/15) if-else.go b/Go/Concepts/15) if-else.go index 6ad22b6..c799c4c 100644 --- a/Go/Concepts/15) if-else.go +++ b/Go/Concepts/15) if-else.go @@ -17,8 +17,6 @@ func main() { fmt.Println("Your number is less than 10") } - // Creating a variable inside the if statement - if 9%2 == 0 { fmt.Println("9 is even") } else { diff --git a/Go/Concepts/16) loops.go b/Go/Concepts/16) loops.go index 31bf8a4..98c0a3f 100644 --- a/Go/Concepts/16) loops.go +++ b/Go/Concepts/16) loops.go @@ -11,7 +11,7 @@ func main() { // } sum := 0 - for i := 0; i < 10; i++ { + for i := 0; i < 10; i++ { //for [initialization]; [condition]; [post] { } sum += i } fmt.Println(sum) @@ -22,16 +22,19 @@ func main() { fmt.Println(days[i]) } + // for [index] := range [array] { } for i := range days { // Better way to iterate over an array fmt.Println(days[i]) } + // for [index], [value] := range [array] { } for i, day := range days { // This is the preferred way to iterate over an array fmt.Println(i, day) // i is the index and day is the value } + // for _, [value] := range [array] { } for _, day := range days { // we can ignore the index by using _ - fmt.Println(day) + fmt.Println(day) } } diff --git a/Go/Concepts/18) break-continue.go b/Go/Concepts/18) break-continue.go index 618a7d8..26e3ea5 100644 --- a/Go/Concepts/18) break-continue.go +++ b/Go/Concepts/18) break-continue.go @@ -4,6 +4,8 @@ import ( "fmt" ) +// continue: Will skip that ireration, but will not break the look unlike "break" + func main() { for i := 0; i < 5; i++ { if i == 3 { // Here 3 will not be printed diff --git a/Go/Concepts/19) function.go b/Go/Concepts/19) function.go index 9163e82..25d68d9 100644 --- a/Go/Concepts/19) function.go +++ b/Go/Concepts/19) function.go @@ -19,7 +19,7 @@ func greetUser() { fmt.Println("Hello, welcome to the app") } -func addTwoNumbers(a int, b int) int { // Function with Para +func addTwoNumbers(a int, b int) int { // Function with Para and retun value return a + b } diff --git a/Go/Concepts/20) methods.go b/Go/Concepts/20) methods.go index 7528e62..283627e 100644 --- a/Go/Concepts/20) methods.go +++ b/Go/Concepts/20) methods.go @@ -2,27 +2,26 @@ package main import "fmt" - -type UserData struct { // struct is a collection of fields. - fistName string - lastName string - email string +type UserData struct { // struct is a collection of fields. + fistName string + lastName string + email string numberOfTickets int } func main() { user := UserData{ // struct initialization - fistName: "John", - lastName: "Doe", - email: "pradumnasaraf@gmail.com", + fistName: "John", + lastName: "Doe", + email: "pradumnasaraf@gmail.com", numberOfTickets: 2, } fmt.Println(user) - user.greetUser() // Method call + user.greetUser() // Method call } func (u UserData) greetUser() { // method with receiver fmt.Println("Hello", u.fistName, u.lastName) -} \ No newline at end of file +} diff --git a/Go/Concepts/23) files.go b/Go/Concepts/23) files.go index 0d27e9b..60c3187 100644 --- a/Go/Concepts/23) files.go +++ b/Go/Concepts/23) files.go @@ -3,14 +3,17 @@ package main import ( "fmt" "io" - "io/ioutil" "os" ) func main() { - content := "Hello, playground!" - file, err := os.Create("./test.txt") + // Process to create, write and read a file + + content := "Hey I am Pradumna" + + //"." means current directory + file, err := os.Create("./hello.txt") if err != nil { panic(err) @@ -21,20 +24,16 @@ func main() { if err != nil { panic(err) } - fmt.Println("Length of the file is: ", length) - defer file.Close() - - readFile("./test.txt") -} - -func readFile(filename string){ + fmt.Println("Length:", length) - databyte, err := ioutil.ReadFile(filename) + data, err := os.ReadFile("./hello.txt") if err != nil { panic(err) + } - fmt.Println("Text data inside a file is \n", string(databyte)) + // data is of type []byte so we need to convert it to string + fmt.Println(string(data)) } diff --git a/Go/Concepts/24) handling-web-req.go b/Go/Concepts/24) handling-web-req.go index 5b3a704..71f9829 100644 --- a/Go/Concepts/24) handling-web-req.go +++ b/Go/Concepts/24) handling-web-req.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "net/http" ) @@ -16,7 +16,7 @@ func main() { fmt.Printf("Response is of type: %T\n", res) fmt.Println(res.Status) - databytes, err := ioutil.ReadAll(res.Body) //We can't read the response directly. So we use ioutil.ReadAll() + databytes, err := io.ReadAll(res.Body) //We can't read the response directly. So we use ioutil.ReadAll() checkNilError(err) content := string(databytes) // convert the byte array to string diff --git a/Go/Concepts/25) handling-url.go b/Go/Concepts/25) handling-url.go index a5b8568..4e49b3a 100644 --- a/Go/Concepts/25) handling-url.go +++ b/Go/Concepts/25) handling-url.go @@ -9,27 +9,26 @@ const myURL = "https://pradumnasaraf.dev:3000/services?service=twitter" func main() { - fmt.Println(myURL) - - // Parsing - + // Parsing the URL results, _ := url.Parse(myURL) - fmt.Println(results.Scheme) - fmt.Println(results.Host) - fmt.Println(results.Port()) - fmt.Println(results.RawQuery) - fmt.Println(results.Query()) + fmt.Println(results.Scheme) // Scheme is the protocol used. Eg: http, https, ftp, etc + fmt.Println(results.Host) // Host is the domain name + fmt.Println(results.Port()) // Port is the port number + fmt.Println(results.RawQuery) // RawQuery is the query string + fmt.Println(results.Query()) // Query is the query string in map form data := results.Query() fmt.Println(data["service"]) + // &url.URL{} is a pointer to a url.URL type partsOfUrl := &url.URL{ - Scheme: "https", - Host: "pradumnasaraf.dev", - Path: "/services", + Scheme: "https", + Host: "pradumnasaraf.dev", + Path: "/services", RawQuery: "user=pradumna", } + // String() method is used to convert the url.URL type to string anotherUrl := partsOfUrl.String() fmt.Println(anotherUrl) } diff --git a/Go/Concepts/26) basic-server.go b/Go/Concepts/26) basic-server.go index 74fe55c..b1d3571 100644 --- a/Go/Concepts/26) basic-server.go +++ b/Go/Concepts/26) basic-server.go @@ -11,6 +11,6 @@ func main() { http.ListenAndServe(":8080", nil) } -func hello(w http.ResponseWriter, r *http.Request) { +func hello(w http.ResponseWriter, r *http.Request) { // w is used to write the response and r is used to read the request w.Write([]byte("Hello World")) } \ No newline at end of file